Nidhi
Nidhi
Nidhi
TECHNOLOGY
Module –6
Shell introduction and Shell Scripting:
•
Learning Objectives
•Imparting UNIX programming skill to students
• Multi-User Systems
Single-User Systems
Multi-user Systems
Multi-user Systems are used by more than one person at a time. Multi-user Systems are
required when a number of programs have to be run simultaneously, or common resources like
printers and disks are to be shared by number of users . A multi-user system consists of a
single computer with several terminals attached to it. This computer can be a PC-AT ,a mini or
a mainframe.
Interface of
Unix OS
• GUI(Graphical User Interface)
LINUX,WINDOWS
MS-DOS,UNIX
The traditional Unix user interface is a simple command-line shell.
1) The shell prints a prompt when it is ready to receive commands. You use
the keyboard to type in a command. Then the shell parses and processes
the command and executes it for you. The command prints its output, if
any, to the same terminal window. When the command is done, the shell
prompts you again to enter another command terminal window has no
graphics capability
This GUI has all the same features that you would find on a personal
computer. In fact, the version used most commonly on modern Unix
systems look very much like Microsoft Windows.
History of UNIX
• UNIX has been a popular OS for more than two decades because of its multi-
user, multi-tasking environment, stability, portability and powerful networking
capabilities.
• UNIX is an operating system which was first developed in the 1960s, and has
been under constant development ever since.
• Ken Thompson then teamed up with Dennis Ritchie, the author of the first C
compiler in 1973. They rewrote the UNIX kernel in C - this was a big step forwards
in terms of the system's portability - and released the Fifth Edition of UNIX to
universities in 1974.
• First operating system written in High Level Language C by Ken Thompson and
Dennis Ritchie
What is UNIX?
• The UNIX operating system is a set of programs that act as a link between
the computer and the user.
• The computer programs that allocate the system resources and coordinate all
the details of the computer's internals is called the operating system or kernel.
• Users communicates with the kernel through a program known as the shell.
• Shell acts as a blanket around the kernel and eliminates the need
for the programmer to communicate directly with the kernel
• Branching from the root there are several other directories called
bin lib,usr,etc,temp,dev
• The root directory also contain a file called UNIX which is UNIX
kernel itself.
• These directories are called sub directories ,their parent being the
root directory
Cont…
bin Binary Executable File contain files for most of the UNIX command and commands can be
either C programs or shell programs
lib Directory contains all the library functions provided by the UNIX for programmers. The
programs written under UNIX make use of these library functions in the lib directory.
dev Device related files that controls various input/output devices like terminals, printers etc
Usr Home directory of all users. In this there are several directoris,each associated with a
particular user. Each user is allowed to work in his own directory
/usr/bin Additional binary executable files. Within the user there is another bin directory which
contains additional UNIX commands files
Creating a directory
• The mkdir(make directory)command is used to create directories
• $ mkdir project
• The subdirectory project is created under the current directory.
• $ mkdir / user/games/project
Removing a directory
• The rmdir(remove directory)removes the directory specified.
• $ rmdir cobol
•$
• where cobol is the directory which is deleted.
• A directory can be deleted only if it is :
• Empty
• Not the current directory.
Managing Directories
rmdir: Remove directory
rmdir() attempts to remove the empty directory with the specified name
‘rmdir’ command is used to remove single or multiple directories/sub-directories
under the existing directory with the specified path.
Syntax $rmdir
“ls” command is used to display the list of files and directories in ascending order
• $ touch filename
Example:
• touch x.txt y.so z.xl
• touch 03161430 z.xl
• touch file{1,2,3,4}
Create ,Display and Delete Files
• If File is already existing,it will not create a new file with the same name
under that directory
• If the file does not exist,then it creates a new file in write mode where we can
insert any content
• Ctrl+d keywords is used to come out of the file after placing the content
$cat>filename
cat>hello
Create ,Display and Delete Files
File creation: vi
Syntax: vi filename
vi aa.sh
vi file1 file2
Create ,Display and Delete Files
File content display:
Cat This command is used to display the content of File(s) and concatenate
the contents
• $more filename
less
• “less” command is similar to “more” command .It allows backward as well as
forward movement in the file with almost “vi” editor commands features
• “less” command does not have to read the entire input file before starting ,so
with large input files it starts up faster than text editor like “vi”
• less filename
Create ,Display and Delete Files
rm: Delete Files
Example:
rm f1 f2 f3
rm –rf prog/f5 prog/f6
rm I dir l/ch[12]
rm*
man - allows users to view the reference manuals of a command or utility run in the
terminal.
◆ Syntax : man [option] [section number] [command name]
◆ Example : man 3 sleep
echo - used for displaying lines of text or string which are passed as arguments on the
command line.
◆ Syntax : echo [option] [string]
◆ Example : $ echo "Welcome to Linux”
printf - used to display the given string, number or any other format specifier on the
terminal window.
◆ Syntax : printf [-v var] format [arguments]
◆ Example : $printf "%s\n" "Hello, World!“
uname - print Unix system information /used to change the user account passwords.
◆ Syntax : uname [OPTION]
◆ Example : $ uname –n
➔ cat - reads data from the file and gives their content as output.
◆ Syntax : cat [OPTION]... [FILE]
◆ Example : $ cat filename.txt
➔ grep - The grep filter searches a file for a particular pattern of characters,
and displays all lines that contain that pattern.
◆ Syntax : grep [options] pattern [files]
◆ Example : $ grep -l "unix" f1.txt f2.txt f3.xt f4.txt
Cont..
• who: tells you who's logged on, and where they're coming from.
• whoami --- returns your username.
• passwd --- lets you change your password.
date --- shows the current date and time.
Formatting options of date:
Date +%A---date “+DATE: %A”
Date +%a
Date +%B Month in capital form
Date +%b
Date +%F
date +%T
date +%y
date +%Y
To set Date
• date “+DATE: %m%d%y%n TIME: %H%M%S”
Shell scripts
• A shell script is a text file with Unix commands in it.
Variables in shell scripts do not have associated data types that is, they
are not declared to be integers or characters. All variables in UNIX are
treated as character strings. It is possible, however, to mathematically
manipulate variables.
Referencing Variables
• The $ symbol is used to refer to the contents of a variable.
• For example, to assign the value of one variable to another, the
command would be:
• variable1=${variable2}
In case no concatenation is involved, the braces are optional
• x=$today
• is the same as
• x=${today}
Reading a value into a variable
• read command.
read var1 var2 var3
this is to test the read
$ echo $var1
this
$ echo $var2
is
$ echo $var3
to test the read
$
The expr Command
• The shell does not support numeric variables.All variables are treated
as
• character strings.Therefore,the declaration
• var1=25
• The use of the expr command is illustrated in the following code:
• A=10
• B=5
• A=’expr’$a-7
• echo “a is equal to $a”
• a=’expr’$a \ *$b’
• echo “a s equal to $a”
• a=’expr’ $a / 3
• echo “a is equal to $a”
• The output of this code would be
• a is equal to 3
• a is equal to 15
• a is equal to 5
Control statements
• Control statements control the flow of execution in a programming
language.
for i in 1 2 3 4 5
do
sum=`expr $sum + $i`
done
echo $sum
Cont…
for name in Ruby Samuel
do
echo “${name}”
done
The UNIX shell offers constructs for looping and decision-
making that can be used in shell scripts. An example is the if…
then…else construct. The if construct is generally used in
conjunction with the test command.
or
n=1
while [ $n –le 10 ]
do
echo $n
n=`expr $n +1`
done
How to read files using while loop
while read p //Here p is the variable where we want to save the content line by line
do
echo $p
done< hello.sh //use angle bracket of < input redirection operator it will read hello.sh
Or
// Read the file in a single variable and then print it if u know pipes it would be easier
a=1
while [ $a -le 5 ]
do
echo “value of a=” $a
a=`expr $a + 1`
done
THANK YOU