Bourne Shell Programming An Introduction
Bourne Shell Programming An Introduction
An Introduction
• Shell variables
• Using Quotes
• Arithmetic On Shell
• Passing Arguments
• Testing conditions
• Branching
– if-else, if-elif, case
• Looping
– while, for, until
– break and continue
1
Some Regular Expression Characters
2
Shell Scripts
• Bourne Shell/Korn Shell
• Invoking a shell script
$shell_script_file or $sh -options shell_script_file
– the script file must have execute-permission.
• Shell Variables
– mydir=/usr/jsmith/bin
– count= #assign a null value to the variable
– echo $mydir #display the contents of mvdir
– x=*
– echo $x #substitutes the names of the files in the directory
3
2
Shell Scripts
#name of a command, options and arguments can be stored inside
variables
command=ls
option=-l
filename=namesFile
$command $option $filename #shell performs the variable substitution
before it
# executes the command.
4
2
Quotes
• The Single Quote
– The white-space characters enclosed between the
single quotes are preserved by the shell.
– The special characters are ignored.
– Example:
filename=/usr/jsmith/bin/prog1
echo $filename
echo ‘$filename’
echo ‘<> | ; () {} ` & “‘
5
Quotes
• The Double Quote
– echo $x #filenames are substituted
– The special characters, $, back quotes (`) and
back slashes (\) are not ignored.
– Example;
– x=*
– echo ‘$x’ #$x is displayed
– echo “$x” # * is displayed, variable substitution
is done inside the double quotes, no file name
substitution is done and * is passed to the
shell.
6
Quotes
• The Back Quote
– purpose is to tell the shell to execute the enclosed
command and to insert the standard output from the
command at that point on the command line.
– Example:
echo The date and time is: `date`
echo There are `who | wc -l` users logged on
filelist=`ls`
echo $filelist (#what is the output)
mail `sort -u names` < memo
#-u option removes the duplicate
# entries from the file
7
Quotes
• The Back Slash
– Is same as putting single quotes around a single
character.
– Quotes the single character that immediately follows
it.
– X=*
– echo \$x # $x is displayed
– Is interpreted inside the double quotes.
– Use backslash inside the double quotes to remove
the meaning of characters that otherwise would be
interpreted.
– Examples:
echo “\$x” #$x is displayed
echo “The value of x is \”$x\””
#The value of x is “5” is displayed 8
Arithmetic On Shell
• A variable is just considered a string of
characters.
– Example:
– x=1
– x=$x+1
– echo $x #will display 1+1
– A unix program expr evaluates an
expression given to it on the command line.
– Each operator and operand given to expr
must be a separate argument. The
operators, +, -, *, /, % are recognized.
9
Arithmetic On Shell
Example:
i=1
i=`expr $i + 1`
– Evaluates only integer arithmetic
expressions.
– awk may be used for floating point
calculations.
expr 10 * 2 # what is the problem with this?
10
Passing Arguments
• $#: Number of arguments passed to the program from the command
line.
• $* : references all the arguments
Example:
%cat showArgs
echo $# arguments passed.
echo they are :$*:
%showArgs a b c d 1 #output -
%showArgs “a b c d 1” #output -
% showArgs `cat names` #output -
% showArgs x* #output -
%cat lookup
grep $1 phonebook
lookup “Mary Jones”
What is the result?
11
Positional Parameters
• Positional parameters
– set shell script arguments. e.g.
$my_script a b xy “1 2 3”
– positional parameters have the values
$0 -- my_script
$1 -- a
$2 -- b
$3 -- xy
$4 -- 1 2 3
– $* - references all the variables passed as arguments
12
The shift command
• shift
– left-shifts the positional parameters.
– If more than 9 arguments are supplied,
arguments 10 and up cannot be
referenced.
– use shift to access these arguments.
– shift assigns value in $2 into $1, $3 into $2
etc.
– The number of arguments ($#) gets
decremented by one on each shift.
13
The shift command
%cat testshift
echo $# $*
shift
echo $# $*
shift
echo $# $*
% cat testshift 1 2 3 4 5 6 7 8 9 10
What is the output?
14
Testing Conditions
• if statement: allows to test a condition and branch on the
exit status of the condition tested.
• An exit status of 0 indicates the program executed
successfully.
• An exit status of non-zero indicates a failure.
• $?: contains the exit status of the last command executed.
• Operators for integer comparisons
– eq (equal), -ge (greater than or equal), -gt
(greater than), le (less than or equal), -lt (less
than) and –ne (not equal)
15
Testing Conditions
• Operators for string comparisons
– = , !=, -n (string is not null) and –z (string is null)
• File operators
– -d file file is a directory
– -f file file is an ordinary file
– -r file file is readable by the process
– -s file is of non-zero length
16
Testing Conditions
Examples
user=$1
who | grep “^$user” > /dev/null
- the exit status of the last
command in the pipe line is
returned.
17
The test command
• The test command is used to test one or more
conditions in an if statement.
y=ABC
test "$y" = ABC
echo $? # displays 0
x=
test -n x #checks if x is not null
echo $? #displays 1
test -z x #checks if string is null
echo $?
18
The test command
• The test command is used to test one or more
conditions in an if statement.
x=ABC
[ "$x" = ABC ] #[] same as using test
x=5
# -a for logical and -o for logical or
[ "$x" -ge 0 -a "$x" -lt 10 ]
[ -f “$file1” -a -r “$file1” ]
19
Branching
%cat isUserOn
#checks if a user is logged on
user=$1
if who | grep “$user” #what is the problem with matching
#a username in the output of who?
then
echo “$user is logged on”
fi
20
Branching
21
Using case
case: allows a comparison of a single character against other
values and execute a command if a match is found.
%cat ctype
x=A
case "$x“ #The value in x is compared with each of the
cases
#until a match is found. When a match is found,
the
#commands up to the double colons are executed.
in
[0-9] ) echo digit;;
[A-Z] ) echo uppercase;;
[a-z ) echo lowercase;;
* ) echo special character;;
esac
22
The && and || constructs
• command1 && command2
– if the exit status of command 1 is 0 then command 2 is
executed.
– Example
EDITOR=
[ -z "$EDITOR" ] && EDITOR=/bin/ed
echo "$EDITOR"
• command1 || command2
– If the exit status of command 1 is not zero, then command 2
is executed.
– Example:
grep “$name” phonebook || echo “Couldn’t find name”
23
Looping
• The for loop is executed for as many words as
are included after in
• for var in listofwords
do
commands
done
for i in 1 2 3
do
echo $i
done
24
Looping
for file in * #substitutes all the files in the
# directory
do
processCmd $file
done
25
The for loop
for var #uses all the arguments given to the program
# on the command line
do
command
command
done
for file in $* # Replaces $1, $2 as $1, $2 etc
do
x=`wc -l $file`
echo There are `echo $x |cut -f1 -d’ ‘` lines in $file
done
for file in “$@” #Replaces $1, $2 as “$1”, “$2” etc.
#Should be included in double quotes
do
echo $file
done 26
Looping
while command
do
command1
command2
done
• command1 and 2 are executed until command returns a nonzero
exit status
# Print command line arguments
while [ “$#” -ne 0 ]
do
echo “$1”
shift
done
27
Looping
until command
do
command1
command2
done
• command1 and command2 are executed as long as command
returns a non-zero exit status.
until who | grep “^$user “
do
sleep 60
done
28
break and continue
• break: to break out of a loop.
• break n: to break out of n inner most loops
for file
do
#variable error can be set to a value
count=1
while [ “$count” -le 5 ]
do
#process file
if [ -n “$error” ] #contains a value
then
break 2
fi
count=`expr $count + 1`
done
done
29
continue
• continue: the remaining commands in the loop are
skipped.
for file
do
if [ ! -f “$file” ]
then
echo “$file not found”
continue
fi
cat $file
done
30