more shell programming. slide 2 control flow the shell allows several control flow statements: if ...
TRANSCRIPT
More Shell Programming
Slide Slide 22
Control Flow
The shell allows several control flow statements: if while for
Slide Slide 33
The if statement works mostly as expected:
$ whoami clinton$ cat test7#!/bin/sh
user=`whoami`if [ $user = "clinton" ]then
echo "Hi Bill!"fi$ test7Hi Bill!
However, the spaces before and after the square brackets [ ] are required.
if
Slide Slide 44
The if then else statement is similar:
$ cat test7#!/bin/sh
user=`whoami`if [ $user = "clinton" ]then
echo "Hi Bill!"else
echo "Hi $user!"fi$ test7Hi horner!
if … then … else
Slide Slide 55
You can also handle a list of cases:
$ cat test8#!/bin/sh
users=`who | wc -l`if [ $users -ge 4 ]then
echo "Heavy load"elif [ $users -gt 1 ]then
echo "Medium load"else
echo "Just me!"fi$ test8Heavy load!
if … elif … else
Slide Slide 66
Boolean Expressions Relational operators:
-eq, -ne, -gt, -ge, -lt, -le
File operators:-f file True if file exists and is not a
directory-d file True if file exists and is a directory-s file True if file exists and has a size > 0
String operators:-z string True if the length of string is zero-n string True if the length of string is nonzeros1 = s2 True if s1 and s2 are the sames1 != s2 True if s1 and s2 are differents1 True if s1 is not the null string
Slide Slide 77
File Operator Example$ cat test9#!/bin/sh
if [ -f letter1 ]then
echo "We have found the evidence!"cat letter1
elseecho "Keep looking!"
fi$ test9We have found the evidence!
Ms. Lewinski:It is getting late. Please order some pizza and stopby my office. We'll tidy up a few more things beforecalling it a night.Thanks!Bill
Slide Slide 88
The while statement loops indefinitely, while the condition is true, such as a user-controlled condition:
$ cat test11#!/bin/shresp="no"
while [ $resp != "yes" ]
doecho "Wakeup [yes/no]?"
read resp done $ test11 Wakeup [yes/no]? no Wakeup [yes/no]? y Wakeup [yes/no]? yes $
while (1)
Slide Slide 99
while can also do normal incrementing loops:$ cat fac#!/bin/shecho "Enter number: "read nfac=1i=1
while [ $i -le $n ]
dofac=`expr $fac \* $i`i=`expr $i + 1`
doneecho "The factorial of $n is $fac"$ facEnter number:5The factorial of 5 is 120
while (2)
Slide Slide 1010
The break command works like in C++, breaking out of the innermost loop : $ cat test12 #!/bin/sh while [ 1 ]doecho "Wakeup [yes/no]?"
read respif [ $resp = "yes" ]then
break;fi
done$ test12Wakeup [yes/no]? noWakeup [yes/no]?yWakeup [yrs/no]?yes$
break
Slide Slide 1111
Keyword Shell Variables
The shell sets keyword shell variables.
You can use (and change) them.HOME The path to your home directory
PATH Directories where the shell looks for executables
USER Your login name
SHELL The name of the shell you are running
PWD The current working directory
PRINTER Can be loaded with your default printer
Slide Slide 1212
Keyword Example
$ cat env#!/bin/shecho "Hi $USER!"echo "Your home is: $HOME"echo "Your path is: $PATH"echo "Your current directory is: $PWD"echo "Your shell is: $SHELL"echo "Your printer is: $PRINTER"$ envHi horner!Your home is: /homes/hornerYour path is:/usr/bin:.:.:/homes/horner/Unix/bin:...Your current directory is: /homes/horner/111Your shell is: /bin/cshYour printer is: csl3
Slide Slide 1313
Readonly Shell Variables
$0 is the name the user typed to invoke the shell script:
$ cat print1#!/bin/shecho "This script is called $0"$ print1
This script is called print1$ ./print1This script is called ./print1$ ~/111/print1This script is called
/homes/horner/111/print1
Slide Slide 1414
Command Line Arguments – (1)
The command line arguments that you call a script with are stored in variables $1, $2, ..., $9.
$ cat args1#!/bin/shecho "The args are $1 $2 $3 $4 $5 $6 $7 $8 $9"$ args1 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
The args are a1 a2 a3 a4 a5 a6 a7 a8 a9
With more than 9 arguments, they are still stored, but they have to be moved using the shift command before they can be accessed.
Slide Slide 1515
Command Line Arguments – (2) Example: How to write a command to swap two files?
$ cat swap#!/bin/shmv $1 /tmp/$1mv $2 $1mv /tmp/$1 $2
$ cat it1contents of file1$ cat it2contents of file2$ swap it1 it2$ cat it1contents of file2$ cat it2contents of file1$
Slide Slide 1616
Command Line Arguments – (3)
$* lists all the command line args:$ cat args2#!/bin/shecho "The args are $*"$ args2 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
The args are a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
$# contains the number of args:$ cat args3#!/bin/shecho "The number of args is $#"$ args3 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
The number of args is 10
Slide Slide 1717
Command Handling Argument - shift (1) The shift command promotes each command line
argument by one (e.g., the value in $2 moves to $1, $3 moves to $2, etc.)
$ cat shiftargs#!/bin/shecho "The args are 0 = $0, 1 = $1, 2 = $2"shiftecho "The args are 0 = $0, 1 = $1, 2 = $2"shiftecho "The args are 0 = $0, 1 = $1, 2 = $2"shift
$ shiftargs arg1 arg2 arg3The args are 0 = shiftarg, 1 = arg1, 2 = arg2The args are 0 = shiftarg, 1 = arg2, 2 = arg3The args are 0 = shiftarg, 1 = arg3, 2 =
The previous $1 becomes inaccessible
Slide Slide 1818
Example: How to write a general version of the swap command for two or more files?
swap f1 f2 f3 ... fn_1 fn
f1 <--- f2f2 <--- f3f3 <--- f4...fn_1 <--- fnfn <--- f1
Command Handling Argument - shift (2)
Slide Slide 1919
shift Example (1)$ cat swap#!/bin/shorig1=$1mv $1 /tmp/$1while [ $2 ]do
mv $2 $1shift
donemv /tmp/$orig1 $1
$ cat it1 it2 it3contents of file1contents of file2contents of file3$ swap it1 it2 it3
$ cat it1 it2 it3contents of file2contents of file3contents of file1
1st Sample Run
Slide Slide 2020
shift Example (2)
$ swap it1 it2 it3$ cat it1 it2 it3
contents of file3contents of file1contents of file2$ swap it1 it2 it3
$ cat it1 it2 it3contents of file1contents of file2contents of file3$ swap it1 it2 $ cat it1 it2contents of file2contents of file1
2nd Sample Run
3rd Sample Run
4th Sample run
Slide Slide 2121
The set command sets the args:$ cat set1#!/bin/shset yat yih saamecho "One is $1, two is $2, three is $3"
$ set1One is yat, two is yih, three is saam
The set command is useful for moving the output of command substitution into the args:
$ dateThu Feb 25 17:06:27 HKT 1999$ cat day#!/bin/shset `date`echo "Today is $3 $2 $6"
$ dayToday is 25 Feb 1999
Command Handling Argument - set
Slide Slide 2222
$$ is the process ID (PID) of the current process (the shell script PID, or the shell PID if interactive).
$ cat pid#!/bin/shecho $$$ pid1154$ pid1156$ pid1157$ echo $$892$ ps
PID TTY TIME CMD 892 pts/0 0:01 csh
Special variable - $$ (1)
Slide Slide 2323
It can be used for temporary file names:
$ cat swap#!/bin/shfile=/tmp/tmp$$mv $1 $filemv $2 $1mv $file $1$ cat it1 it2
contents of file1contents of file2$ swap it1 it2$ cat it1contents of file2contents of file1$
Special variable - $$ (2)
Slide Slide 2424
for The for statement executes a loop
once for each of a list of possibilities:$ cat printall#!/bin/shfor file in *do
if [ -f $file ]then
echo "Print $file [y/n]? "read respif [ $resp = "y" ]then
lpr –Pcll2a $filefi
fidone
$ printallPrint it1 [y/n]?yPrint it2 [y/n]?n
Slide Slide 2525
The in clause of the for statement accepts as many parameters as you wish in many forms:
$ for i in 1 2 3 ; do echo $i ; done123
$ for pid in `ps -a | tail +2 | cut -c1-6 | sort`> do> kill -9 $pid> donekill: permission deniedkill: permission deniedkill: permission denied
(you will then be logout!)
Looping using for
Slide Slide 2626
For example (2)
for index in 1 2 3 4 5 6 7 8 9 10
do
if [ $index –le 3 ]; then
echo continue
continue
fi
echo $index
if [ $index –ge 8 ]; then
echo “break”
break
fi
done
Slide Slide 2727
Catch a signal: builtin trap
Built-in trap Syntax: trap ‘commands’ signal-numbers Shell executes the commands when it catches one of the signals Then resumes executing the script where it left off.
– Just capture the signal, not doing anything with it trap ‘ ‘ signal_number
Often used to clean up temp files Signals
– SIGHUP 1 disconnect line– SIGINT 2 control-c– SIGKILL 9 kill with -9– SIGTERM 15 default kill– SIGSTP 24 control-z– …
Slide Slide 2828
Example
[ruihong@dafinn ~/cs3451]$ cat inter
#!/bin/sh
trap 'echo PROGRAM INTERRUPTED' 2
while true
do
echo "programming running."
sleep 1
done