7/27/2019 Unit2_Unix_OS31.pdf
1/40
1
UnixProgramming with the Shell.
Welcome to this course on Unix Operating System, Day 2.
7/27/2019 Unit2_Unix_OS31.pdf
2/40
2
2ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Road Map
Recap of Day 1
To understand the types of shell.
To understand Shell variables.
To understand the programming constructs.
To understand the System Variables.
To customize the shell.
To understand Commands related to File Handling.
Shell Programming.
To understand
Types of Shells.
Shell Variables.
Programming Constructs.
System Variables
Customizing the shell
To understand Commands related to File Handling.
7/27/2019 Unit2_Unix_OS31.pdf
3/40
3
3ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Shell Types in Unix
Bourne Shell.
Bourne Again Shell (bash).
C Shell (c-shell).
Korn Shell (k-shell).
TC Shell (tcsh)
Bourne shell (/bin/sh)
This is the original Unix shell written by Steve Bourne of Bell Labs. It is available on allUNIX systems.
This shell does not have the interactive facilities provided by modern shells such asthe C shell and Korn shell.
The Bourne shell does provide an easy to use language with which you can writescripts.
Bourne again shell (/bin/bash)
written by the Free Software Foundation.
This shell is widely used within the academic community.
bash provides all the interactive features of the C shell (csh) and the Korn shell (ksh).Its programming language is compatible with the Bourne shell (sh).
7/27/2019 Unit2_Unix_OS31.pdf
4/40
4
4ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Shell Types in Unix (Contd)
Please see the notes page for an explanation of the earlier slide
C shell (/bin/csh)
This shell was written at the University of California, Berkeley. It provides a C-likelanguage with which to write shell scripts.
Korn shell, /bin/ksh, David Korn (Bell Labs)
This shell was written by David Korn of Bell labs. It is now provided as the standardshell on Unix systems.
It provides all the features of the C and TC shells together with a shell programminglanguage similar to that of the original Bourne shell.
TC Shell (tcsh)
It provides all the features of the C shell together with emacs style editing of thecommand line.
References
http://unixhelp.ed.ac.uk/
7/27/2019 Unit2_Unix_OS31.pdf
5/40
5
5ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
The Shell.
A program that interprets users requests to run programs.
Provides environment for user programs.
A command line interpreter that
Reads user inputs.
Executes commands.
Ask the audience to come out with the different functions and uses of shell, covered inChapter 1.
Shell acts as an interpreter for users requests.
7/27/2019 Unit2_Unix_OS31.pdf
6/40
6
6ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
The Shell.
Shell allows three types of commands:
An internal command.
An executable file that contains a sequence of shell command lines.
An executable file that contains object code produced by compilation.
Three types of commands are allowed in Unix:
1) An internal command refers to the vast l ibrary of in-built commands.
Example : cal, cat, etc.
2) An executable file could be a file created by a user for a particular application, whichcontains various commands.
It is always advisable to have an extension of .sh for the user-program.
A program in Unix created by the user is called as Shell Script.
3) An executable file whose source code could be statement written in languages likeC, etc.
7/27/2019 Unit2_Unix_OS31.pdf
7/40
7
7ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Shell Features.
Interactive and background processing.
Input/output redirection
Pipes.
Wild card matching.
Programmable.
Shell Variables.
Programming language constructs.
Shell Scripts.
Shell allows execution of tasks in the background by putting an & sign.
Example
$> sleep 100
Output : For a period of 100 seconds, the terminal would be idle and user cannot work.
$> sleep 100 & # & is used for placing the process in
the background.[1] 32389 # PID of the process
which has been put in the background.
Output : The command is executed in the background. The user could work on anotherjob.
Shell allows the user to work with pipes, wherein the user can redirect commands.
Shell allows the user to work with Wild Cards, wherein the user can select multiplefiles.
The user could write programs based on the users application.
7/27/2019 Unit2_Unix_OS31.pdf
8/40
8
8ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Shell Variables.
Positional Parameters.
Special Parameters.
Named variables
In shell programming, we deal with three basic types of variables .
Positional parameters
Special parameters
Named variables
7/27/2019 Unit2_Unix_OS31.pdf
9/40
9
9ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Positional Parameters.
Acquire values from the position of arguments in command line.
$1, $2, $3,..$9
sh file1 10 20 30
$1 $2 $3
Acquire values from the position of arguments in command line.
Example:
$ cat>data1
echo Your marks in Sem1 is : $1
echo Your marks in Sem2 is : $2
echo Your marks in Sem3 is : $3
{ctrl - d}
$ sh data1 10 20 30
Your marks in Sem1 is : 10
Your marks in Sem2 is : 20
Your marks in Sem3 is : 30
7/27/2019 Unit2_Unix_OS31.pdf
10/40
10
10ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Special Parameters.
Shell assigns the value for this parameter.
$$ - PID number.
$# - Number of Command Line Arguments.
$0 Command Name.
$* - Displays all the command line arguments.
$? Exit Status.
$- - Shell options
$! - Process number of the last background command
$@ - Same as $*, except when enclosed in double quotes.
Shell assigns the value for the special parameter.
symbol meaning
$$ The process number(PID) of the current shell
$# The number ofpositional parameters while invoking the shell script
$0 The name of thecommand being executed
$* The list of positionalparameters
$? Exit status of the lastexecuted command
$- Shell options
$! The process numberof the last background command
$@ Same as $*, exceptwhen enclosed in double quotes
Example
$> echo $$
32344 #Displays the PID ofthe users shell.
7/27/2019 Unit2_Unix_OS31.pdf
11/40
11
11ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Named Variables.
User-defined variable that can be assigned a value.
Used extensively in shell-scripts.
Used for reading data, storing and displaying it.
Named variables
A named variable is a user-defined variable that can be assigned a value within aShell program.
The value of a named variable can be retrieved by preceding the variable name with a$ sign
Example:$ x1=10
$ echo $x1
10
7/27/2019 Unit2_Unix_OS31.pdf
12/40
12
12ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Accepting Data.
read.
Accepts input from the user.
Syntax : read variable_name.
Example : read sname
VariableName
In shell scripts , read is used for accepting data from the user and storing it for otherprocessing.
7/27/2019 Unit2_Unix_OS31.pdf
13/40
13
13ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Display Data.
echo
Used to display a message or any data as required by the user.
echo [Message, Variable]
Example:
echo Infosys Technologies Ltd.
echo $sname
VariableName
Echo is used for displaying data.
This data could be either a data inside the variable or any message.
7/27/2019 Unit2_Unix_OS31.pdf
14/40
14
14ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Program Execution.
sh
Command to execute program in Unix.
Syntax : sh
Example : sh file1
$> cat>file1
echo "Enter your name"
read sname
echo "Your name is "$sname
{ctrl-d}
$> sh file1
Enter your name
Ajit
Your name is Ajit
Here the value of name is stored in the variable sname.
If a file does not have an executable permission, then the file could be executed usingthe . Operator.
Example
$> . file1.sh #Executes the file named file1.sh
If a file has an executable permission, then the file could be executed only by calling itsname.
Example
$> file1.sh #Executes the file named file1.sh
7/27/2019 Unit2_Unix_OS31.pdf
15/40
15
15ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
test command.
Used extensively for evaluating shell script conditions.
It evaluates the condition on its right and returns a true or false exit status.
The return value is used by the construct for further execution.
In place of writing test explicitly, the user could also use [ ].
7/27/2019 Unit2_Unix_OS31.pdf
16/40
16
16ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
test command (Contd).
Operators used with test for evaluating numeral data are:
-eq Equal To
-lt Less than
-le Less than
-gt Greater than
-ge Greater than or equal to
-le Less than or equal to
$> a1=10
$> a2=20
$> test $a1 -eq $a2
$> echo $?
1
Instead of test command, we could also use [ ]
$> a1=10
$> a2=20
$> [ $a1 -eq $a2 ]
$> echo $?
1
Note : A space is mandatory after [ and before ].
7/27/2019 Unit2_Unix_OS31.pdf
17/40
17
17ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
test command (Contd).
Operators used with test for evaluating string data are:
str1 = str2 True if both equals
str1 != str2 True if not equals
-n str1 True if str1 is not a null string
-z str1 True if str1 is a null string
$> str1="Infosys"
$> str2="Infy"
$> test $str1 = $str2
$> echo $?
1 # indicates both thestrings are different.
7/27/2019 Unit2_Unix_OS31.pdf
18/40
18
18ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
test command (Contd).
Operators used with test for evaluating file data are:
-f file1 True if file1 exists and is a regular file.
-d file1 True if file1 exists and is directory.
-s file1 True if file1 exists and has size greater than 0
-r file1 True if file1 exists and is readable.
-w file1 True if file1 exists and is writable.
-x file1 True if file1 exists and is executable.
$> test -f grt1
$> echo $?
0 #indicates the file by name grt1 exists and isregular file.
7/27/2019 Unit2_Unix_OS31.pdf
19/40
19
19ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Logical Operators.
Logical Operators used with test are:
! Negates the expression.
-a Binary and operator.
-o Binary or operator.
Logical operations
Examples:
$> cat>pr1.sh
x=10;
if test $x -lt 15 -o $x -gt 5 #Checks for value of x greater than 5 or lessthan 15.
then
echo "Correct"
else
echo "Wrong"
fi
{ctrl-d}
Output
Correct.
7/27/2019 Unit2_Unix_OS31.pdf
20/40
20
20ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Shell Quotes-Meanings.
(single forward quotes)
'....' take .... Literally
` ` (single backward quotes)
`` to be interpreted by shell
" " (double quotes)
"...." take .... literally after $.
Examples:
$> x=10
$> echo 'echo $x
echo $x #Output when single quote isused.
$> echo `echo $x`
10 #Output when single backwardquote is used.
$> echo "echo $x"
echo 10 #Output when double quote isused.
7/27/2019 Unit2_Unix_OS31.pdf
21/40
21
21ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
expr command.
Used for evaluating shell expressions.
Used for arithmetic and string operations.
Example : expr 7 + 3
would give an output 10.
When used with variables, back quotes need to be used.
Operator has to be preceded and followed by a space.
$> x=10
$> y=20
$> z=`expr $x + $y`
$> echo $z
30
7/27/2019 Unit2_Unix_OS31.pdf
22/40
22
22ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Conditional Execution.
&&
The second command is executed only when first is successful.
command1 && command2
||
The second command is executed only when the first is unsuccessful.
command1 || command2
$> cat file2
8000
9000
7000
$> grep 9000 file2 && echo "Correct"
9000
Correct
$> grep 2000 file2 || echo "Wrong"
Wrong
7/27/2019 Unit2_Unix_OS31.pdf
23/40
23
23ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Program Constructs
if
for
while
until
case
7/27/2019 Unit2_Unix_OS31.pdf
24/40
24
24ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
if statement.
Syntax.
if control command
then
else
fi
Example:
echo "Enter a number"
read num
if test $num -le 30
then
echo "Number is less than or equal to 30"
else
echo "Number is greater than 30"
fi
Output:
If the user gives a number less than or equal to 30 , then the output would be Numberis less than or equal to 30, else the output would be Number is greater than 30.
7/27/2019 Unit2_Unix_OS31.pdf
25/40
25
25ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
for statement.
Syntax.
for variable-name in value1 value2 ....
do
done
$> cat grt1
for x in 1 2 3 6
do
echo "$x"
done
{ctrl-d}
$> sh grt1
1
2
3
6
7/27/2019 Unit2_Unix_OS31.pdf
26/40
26
26ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
while statement.
Syntax.
while control command
do
done
Example:
$> cat > grt2
x=1
while test $x -le 6
do
echo "$x"
x=` expr $x + 1 `
done
{ctrl-d}
$> sh grt2
1
2
3
45
6
7/27/2019 Unit2_Unix_OS31.pdf
27/40
27
27ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
until statement.
Syntax.
untilcontrol command
do
done
$> cat>grt3
x=1
until test $x -ge 6
do
echo "$x"
x=` expr $x + 1 `
done
{ctrl-d}
$> sh grt3
1
2
3
4
5
7/27/2019 Unit2_Unix_OS31.pdf
28/40
28
28ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
case statement.
Syntax.
case value in
choice1) commands ;;
choice2) commands ;;
....
....
esac
The symbols ;; are used as option terminators.
Example:
$> cat>grt5
echo "Enter the data"
read data
case $data in
1) echo "One";;
2) echo "Two";;
3) echo "Three";;
*) echo "Greater than Three"
esac
{ctrl-d}
Output:
If the user enters any number less than 4, it displays the number in words, else itdisplays Greater than Three.
7/27/2019 Unit2_Unix_OS31.pdf
29/40
29
29ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Useful Shell Scripting commands.
break
To come out of a loop.
continue
To jump to the start of loop.
exit
To prematurely terminate a program.
: or #
To interpret the rest of line as comments.
7/27/2019 Unit2_Unix_OS31.pdf
30/40
30
30ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Trap command.
trap
To alter the effects of certain events that generate signals, which generally tends to
halt a process.
Syntax:
trap command signal list
UNIX provides the trapcommand which allows the user to alter the effects of certain events thatgenerate signals, which generally tend to halt a process:
The hang-upsignal is denoted by code number 1.
The interrupt (Ctrl-C) signal is denoted by code number 2.
The terminate (by kill command) is denoted by code number 15.
The general form of the trapcommandThe trap command, once it has been read in, lies in wait for those signals mentioned in thesignal list. When such a signal shows up, the command is performed instead of theconsequence of the signal that would have been to halt the process.
Example
$> cat>unix_prog1.sh
trap 'echo "You cannot exit without entering data"' 2 #If user presses ^C key, it displaysthe message.
echo "Enter your data"
read data
{ctrl-d}
$> sh unix_prog1.shEnter your data
#Data is requested from the user.
You cannot exit without entering data #User presses ^Cwhen requested for data.
Infosys#User enters the data.
Note : The signal 9 cannot be trapped.
7/27/2019 Unit2_Unix_OS31.pdf
31/40
31
31ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
export command.
export
To make a variable a part of environment and also be accessible to the child shell.
export
A Shell variable can be exportedso that it becomes part of theenvironment. A process canthen access and use any of the variables in its environment.
Note :An exported variable becomes part of the environment of the current shell and of allprocesses descendingfrom it. Also, it is important that to make a variable accessible to allsubshells, it should be defined and exported from the login shell itself.
A user variable if not exported will not be visible to the subshells.
Example:
$> a=10
$> echo $$ #PID of the shell
6034
$> echo $a
10
$> sh #Creates a child shell
$> echo $$ #PID of child shell
9072
$>echo $a
After using export
$> export a
$> sh #Creates a child shell
$>echo $a
10
7/27/2019 Unit2_Unix_OS31.pdf
32/40
32
32ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
System Variables.
PATH
Search path referred by Unix for any command.
echo $PATH
HOME
Indicates the home directory for the user.
echo $HOME
$> echo $PATH #To display the PATH
/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/ajit:.
$> PATH=$PATH:/home #To append to the existing path.
$> echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/ajit:.:/home
HOME
$> echo $HOME #To display the home directory.
/home/ajit
7/27/2019 Unit2_Unix_OS31.pdf
33/40
33
33ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
System Variables (Contd).
PS1
Used for displaying & changing the primary prompt.
echo $PS1
PS2
Used for changing the secondary prompt.
PS1
Example
$> PS1=Infosys> #To change theprimary prompt.
Infosys>
#The prompt changes to Infosys.
PS2
Example
$> PS2=* #The secondary prompt changes to *.
7/27/2019 Unit2_Unix_OS31.pdf
34/40
34
34ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
set command.
set command
Used for display all the environment variables.
Shows the current values of system variables.
Also allows conversion of arguments into positional parameters.
Syntax : set
Example
$> set 12 23 34 #The data 12, 23 and 34 are arguments forset command.
$> echo $1
12
$> echo $2
23
$> echo $334
$> set # set command, usedwithout any arguments, will display the environmental variables.
BASH=/bin/bash
BASH_VERSINFO=([0]="2" [1]="05b" [2]="0" [3]="1" [4]="release" [5]="i386-redhat-
linux-gnu")
BASH_VERSION='2.05b.0(1)-release'
COLORS=/etc/DIR_COLORS
COLUMNS=80
DIRSTACK=()
EUID=538
GROUPS=()
G_BROKEN_FILENAMES=1
PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/ajit/bin:/home/ajit:.PIPESTATUS=([0]="0")
PPID=331
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
PVM_ROOT=/usr/share/pvm3
PVM_RSH=/usr/bin/rsh
PWD=/home/ajit
7/27/2019 Unit2_Unix_OS31.pdf
35/40
35
Unix.File Handling Commands.
7/27/2019 Unit2_Unix_OS31.pdf
36/40
36
36ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
File Handling Commands.
ulimit
Prints the value of resource limits.
ulimit [OPTIONS] [N]
du
Prints the disk usage.
du [OPTIONS] [FILE]
ulimit - Prints the value of resource limits.
Print the value of one or more resource limits, or, ifn is specified, set a resource limit ton.
Resource limits can be either hard (-H) or soft (-S). By default, ulimit sets both limits or printsthe soft limit.
Options:
-H Hard limit. Anyone can lower the hardlimit; only privileged users can raise it.
-S Soft limit. Must be lower than the hardlimit.
-a Print all limits
-c Maximum block size of core files
-n Maximum file descriptor plus 1
du - Print disk usage.
Prints the number of blocks used by each named directory and its subdirectories. Default is thecurrent directory.
Options:
-a Print usage for all files, not just subdirectories
-r Print cannot open message if a file or directory isinaccesible
-s Print only the grand total for each named directory
Example:
$> du s /home/data
To receive the result for the entire data directory as a summary
7/27/2019 Unit2_Unix_OS31.pdf
37/40
37
37ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
File Handling Commands (Contd).
find
Search for files.
find [PATH] [EXPRESSION]
file
File Type determination.
file [OPTIONS] FILES
find - Search for files.
Most commonly used expressions:
-mount Do not descend directories on the file systems
-print Print the current file name
-exec command Execute command
-user uname File is owned by uname
-type t File has a type of t, such as d for directory and ffor file
- name patternLook for the file name of pattern-newer file File was modified more recently than file
Example:
$> find /home name data
Output:
This searches the path /home looking for the name data.
$> find /home name data1 exec rm {}\;
Output:
This searches the path /home looking for the name data1. It removes all the occurences of the file data1.
file - File Type determination.
classify the named files according to the type of data they contain.
Options:
-c check the format of the magic file (files argument is invalid with c)-flist Run file on the filenames in list
-h Dont follow symbolic links
Example:
$> file prog1.c
Output:
Describes the file command as a C Program text.
7/27/2019 Unit2_Unix_OS31.pdf
38/40
38
38ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
File Handling Commands (Contd).
df
Prints disk free blocks.
DF [OPTIONS] [NAME]
touch.
Updates the access and modification times.
touch [-amc] [mmddhhmm[yy]] files
df - Print disk free blocks.
name can be a device name (eg., /dev/dsk/0s9, the directory name of a mountingpoint (e.g., /usr), a directory name, or a remote resource name (e.g., an RFS/ NFSresource).
Options:
-b Print only the number of freeKilobytes
-e Print only the number of free files
-f Report free blocks but not freeinodes
-l Report only on local file systems
-t Report total allocated space as wellas free space
touch - updates access and modification times of a file[mm month, dd date, hh hour, mm minutes, yy year]
If no time is specified, the current time is used.
Option -a updates only the access time.Option -m updates only the modification time.
Option -c prevents creating the file if it did not exist previously.
Example:
$ touch -a test.c
$ touch -m 1221101395 test.c
7/27/2019 Unit2_Unix_OS31.pdf
39/40
39
39ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Summary
Types of Shells
Shell Variables
Program Constructs
System Variables.
File Handling Commands.
7/27/2019 Unit2_Unix_OS31.pdf
40/40
40ER/CORP/CRS/OS31/003
Version No: 2.00Copyright 2004, Infosys
Technologies Ltd
Thank You!