test
in Scriptsif ... then ... else ...
Constructif ... then ... elif ...
Constructfor ... in ...
Loopsfor
Loopsfor
Loopswhile
Loopsuntil
Loopscontinue
break
for
Loop ExamplesI have posted homework 11 here.
This will be the last homework assignment.
It is due this coming Sunday at 11:59 PM.
Are there any questions before I begin?
test
in Scriptstest
...test
test $n1 -gt $n2
or
[ $n1 -gt $n2 ]
test
returns true or false through the exit statustest
returns an exit status of 0test
returns an exit status of 1
Usage: PROGRAM_NAME ARG1 ARG2 ...
basename
...
if [ $# -gt 0 ]
then
dir=$1
else
echo Usage: $(basename $0) DIR_NAME
exit 1
fi
if ... then ... else ...
Constructif ... then ... else ...
construct ...
if COMMAND
then
COMMAND_1
COMMAND_2
...
else
COMMAND_A
COMMAND_B
...
fi
if ... then ... elif ...
Constructif ... then ... elif ...
construct allows you to
create nested if
statements
if COMMAND
then
COMMAND_1
COMMAND_2
...
elif ANOTHER_COMMAND
then
COMMAND_A
COMMAND_B
...
...
[else
COMMAND_N1
COMMAND_N2
...]
fi
elif
stands for "else if"elif
must be followed by then
then
must either be on the next line ...else
statement must be terminated by a fi
elif
only requires a single fi
at the end$ cat match_three.sh #! /bin/bash # # takes three stings as input and compares them if [ $# -lt 3 ] then echo Usage: $(basename $0) string1 string2 string3 exit 1 fi if [ "$1" = "$2" -a "$2" = "$3" ] then echo "All arguments match" elif [ "$1" = "$2" ] then echo "Arguments 1 and 2 match" elif [ "$1" = "$3" ] then echo "Arguments 1 and 3 match" elif [ "$2" = "$3" ] then echo "Arguments 2 and 3 match" else echo "No arguments match" fi $ bash -x match_three.sh foo bar foo + '[' 3 -lt 3 ']' + '[' foo = bar -a bar = foo ']' + '[' foo = bar ']' + '[' foo = foo ']' + echo 'Arguments 1 and 3 match' Arguments 1 and 3 match
for ... in ...
Loopsif
statement is the loopfor ... in
loop
for LOOP_VARIABLE in LIST_OF_VALUES
do
COMMAND_1
COMMAND_2
...
done
do
must be on a different line from for
...do
...
then
in an if
statementdo
and done
are repeated each time through the loopfor ... in
loop, Bash
do
and done
do
and the done
again
$ cat fruit.sh
#! /bin/bash
#
# demonstrates the for in loop
for fruit in apples oranges pears bananas
do
echo $fruit
done
echo Task complete.
$ ./fruit.sh
apples
oranges
pears
bananas
Task complete.
for
Loopsfor
loop is simpler than the for ... in ...
loop
for LOOP_VARIABLE
do
COMMAND_1
COMMAND_2
...
done
for
loops is where they get the valuesfor ... in ...
loop gets values from the list that follows in
for
loop is runfor
loop, in contrast ... for
loop can have different values each time it is run$ cat for_test.sh #! /bin/bash # # demonstrates the simple for loop for arg do echo $arg done $ ./for_test.sh foo bar bletch foo bar bletch $ ./for_test.sh bing bang boom bing bang boom
for
Loopsfor
loops above are very differentfor
statement
for
statements in most programming languages create the values used in the loopfor
loops we saw above ...in
for
loop in Bash
for (( STMT_1; STMT_2; STMT_3 ))
do
COMMAND_1
COMMAND_2
...
done
$ cat count_to_five.sh
#! /bin/bash
#
# this script demonstrates the three statement for loop
for (( count=1; count<=5; count++ ))
do
echo $count
done
$ ./count_to_five.sh
1
2
3
4
5
while
Loopsfor
loops keep running ...while
loop continues running ...while
...while
loops have the form
while COMMAND
do
COMMAND_1
COMMAND_2
...
done
do
and done
will be run$ cat count_to_nine.sh #! /bin/bash # # counts to 9, then stops number=0 while [ $number -lt 10 ] do echo -n $number (( number += 1)) done echo $ ./count_to_nine.sh 0123456789
echo
command in the while loop uses the -n option ...echo
commandwhile
loop tests must change ...while
loop will run forever
$ cat forever.sh #! /bin/bash # # this loop runs forever number=0 while [ $number -lt 10 ] do echo $number done $ ./forever.sh 0 0 0 0 ^C
test
never becomes false#!/bin/bash # # prints something to the terminal 1000 times count=0 while [ $count -lt 1000 ] do sleep 5 echo Excuse me (( count++ )) done
until
Loopsuntil
loop is similar the while
loop until
loop ends ...while
loop stops ...until
loop has the form
until COMMAND
do
COMMAND_1
COMMAND_2
...
done
$ cat count_until.sh #! /bin/bash # # counts from 1 to its argument, then stops if [ $# -eq 0 ] then echo Usage: $(basename $0) NUMBER exit 1 fi number=1 until [ $number -gt $1 ] do echo $number (( number += 1)) done $ ./count_until.sh 10 1 2 3 4 5 6 7 8 9 10 $
while
loops are used much more often than until
loopscontinue
do
and done
continue
was createdcontinue
inside a loop
$ cat continue.sh
#! /bin/bash
#
# demonstrates how continue works
total=0
for number in 1 2 3 4 5
do
if [ $number -eq 2 -o $number -eq 4 ]
then
continue
fi
echo Adding $number to $total
(( total += $number ))
done
echo
echo total: $total
$ ./continue.sh
Adding 1 to 0
Adding 3 to 1
Adding 5 to 4
total: 9
continue
does not cause the script to break out of the loopbreak
for
loops end when every list value is usedwhile
and until
loops end when a logical condition is metfor
loop break
$ cat break.sh
#! /bin/bash
#
# demonstrates how break works
for filename in *
do
if [ -x $filename ]
then
echo First executable file: $filename
break
fi
done
$ ./break.sh
First executable file: bother.sh
continue
jumps out of the loop for one iterationbreak
will exit the loop completelyfor
Loop Examplesfor
loop often when doing administrative work
for my classes
finger
on a username to get the name behind each username
$ finger it244gh Login: it244gh Name: Glenn Hoffman Dummy Directory: /home/it244gh Shell: /bin/bash Last login Sun Dec 8 19:29 (EST) on pts/34 from 209.6.202.123 Mail forwarded to glennhoffman@mac.com No mail. Plan: This account is a test account for Glenn Hoffman teaching it244
head -1
$ finger it244gh | head -1 Login: it244gh Name: Glenn Hoffman Dummy
$ for username in * > do > finger $username | head -1 > done Login: aburch96 Name: Austin Burch Login: ancox Name: Andrew Cox Login: antony11 Name: Antony Karanja ...
svn update
pushd $wksp &> /dev/null for entry in * do if [ -d $entry ] then echo $entry cd $entry pwd svn update cd .. fi done popd &> /dev/null