Day1.5 ShellProgramming
Day1.5 ShellProgramming
Programming
Shell Scripts (1)
• Basically, a shell script is a text file with Unix
commands in it.
• Shell scripts usually begin with a #! and a shell name
– For example: #!/bin/sh
– If they do not, the user's current shell will be used
• Any Unix command can go in a shell script
– Commands are executed in order or in the flow
determined by control statements.
• Different shells have different control structures
– The #! line is very important
– We will write shell scripts with the Bourne shell (sh)
Shell Scripts (2)
• Why write shell scripts?
– To avoid repetition:
• If you do a sequence of steps with standard Unix
commands over and over, why not do it all with
just one command?
fi
• Example:
if grep unix myfile >/dev/null
then
echo "It's there"
redirect to /dev/null so that
fi "intermediate" results do not get
printed
if and else
if grep "UNIX" myfile >/dev/null
then
echo UNIX occurs in myfile
else
echo No!
echo UNIX does not occur in myfile
fi
if and elif
if grep "UNIX" myfile >/dev/null
then
echo "UNIX occurs in file"
elif grep "DOS" myfile >/dev/null
then
echo "Unix does not occur, but DOS does"
else
echo "Nobody is there"
fi
Use of Semicolons
• Instead of being on separate lines, statements
can be separated by a semicolon (;)
– For example:
if grep "UNIX" myfile; then echo "Got it"; fi
– This actually works anywhere in the shell.
% cwd=`pwd`; cd $HOME; ls; cd $cwd
Use of Colon
• Sometimes it is useful to have a command which does
“nothing”.
• The : (colon) command in Unix does nothing
#!/bin/sh
if grep unix myfile
then
:
else
echo "Sorry, unix was not found"
fi
The test Command – File Tests
• test –f filedoes file exist and is not a directory?
• test -d file does file exist and is a directory?
• test –x file does file exist and is executable?
• test –s file does file exist and is longer than 0 bytes?
#!/bin/sh
count=0
for i in *; do
if test –x $i; then
count=`expr $count + 1`
fi
done
echo Total of $count files executable.
The test Command – String Tests
• test –z string is string of length 0?
• test string1 = string2 does string1 equal string2?
• test string1 != string2 not equal?
• Example:
if test -z $REMOTEHOST
then
:
else
DISPLAY="$REMOTEHOST:0"
export DISPLAY
fi
The test Command – Integer Tests
• Integers can also be compared:
– Use -eq, -ne, -lt, -le, -gt, -ge
• For example:
#!/bin/sh
smallest=10000
for i in 5 8 19 8 7 3; do
if test $i -lt $smallest; then
smallest=$i
fi
done
echo $smallest
Use of [ ]
• The test program has an alias as [ ]
– Each bracket must be surrounded by spaces!
– This is supposed to be a bit easier to read.
• For example:
#!/bin/sh
smallest=10000
for i in 5 8 19 8 7 3; do
if [ $i -lt $smallest ] ; then
smallest=$i
fi
done
echo $smallest
The while Loop
• While loops repeat statements as long as the next
Unix command is successful.
• For example:
#!/bin/sh
i=1
sum=0
while [ $i -le 100 ]; do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo The sum is $sum.
The until Loop
• Until loops repeat statements until the next
Unix command is successful.
• For example:
#!/bin/sh
x=1
until [ $x -gt 3 ]; do
echo x = $x
x=`expr $x + 1`
done
Command Line Arguments (1)
• Shell scripts would not be very useful if we could not
pass arguments to them on the command line
• Shell script arguments are “numbered” from left to
right
– $1 - first argument after command
– $2 - second argument after command
– ... up to $9
– They are called “positional parameters”.
Command Line Arguments (2)
• Example: get a particular line of a file
– Write a command with the format:
getlineno linenumber filename
#!/bin/sh
head -$1 $2 | tail -1
• Other variables related to arguments:
• $0 name of the command running
• $* All the arguments (even if there are more than
9)
• $# the number of arguments
Command Line Arguments (3)
• Example: print the oldest files in a directory
#! /bin/sh
# oldest -- examine the oldest parts of a directory
HOWMANY=$1
shift
ls -lt $* | tail +2 | tail $HOWMANY
• The shift command shifts all the arguments to the left
– $1 = $2, $2 =$3, $3 = $4, ...
– $1 is lost (but we have saved it in $HOWMANY)
– The value of $# is changed ($# - 1)
– useful when there are more than 9 arguments
• The “tail +2” command removes the first line.
More on Bourne Shell Variables (1)
• There are three basic types of variables in a
shell script:
– Positional variables ...
• $1, $2, $3, ..., $9
– Keyword variables ...
• Like $PATH, $HOWMANY, and anything else we
may define.
– Special variables ...
More on Bourne Shell Variables (2)
• Special variables:
– $*, $# -- all the arguments, the number of
the arguments
– $$ -- the process id of the current shell
– $? -- return value of last foreground
process to finish
-- more on this one later
– There are others you can find out about with man
sh
Reading Variables From Standard
Input (1)
• The read command reads one line of input from the
terminal and assigns it to variables give as
arguments
091286899 90 H. White
197920499 80 J. Brown
899268899 75 A. Green
……