test in Scriptsif ... then ... else ... Constructif ... then ... elif ... Constructfor ... in ... Loopsfor Loopsfor Loopwhile Loopsuntil Loopscontinuebreakfor Loop ExamplesAre there any questions before I begin?
I have posted homework 11 here.
This will be the last homework assignment.
It is due this coming Sunday at 11:59 PM.
You can connect to Gradescope to take weekly graded quiz today during the last 15 minutes of the class.
Once you start the quiz you have 15 minutes to finish it.
You can only take this quiz today.
There is no makeup for the weekly quiz.
if ... elif ... else Clause Will Runif ... elif ... else statement if ... elif ... else statement with 2
elif clauses
if [ -f xxxx ]
then
echo Found xxxx
elif [ -f foo1.txt ]
then
echo Found foo1.txt
elif [ -f foo2.txt ]
then
echo Found foo2.txt
fi
$ ls -1 foo?.txt foo1.txt foo2.txt ...
$ ./elif_test.sh Found foo1.txt
elif condition were true ...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 0 ...test returns an exit status of 1if ... then ... else ... Constructif statement is the
if ... then ... else ... ...
if COMMAND
then
COMMAND_1
COMMAND_2
...
else
COMMAND_A
COMMAND_B
...
fi
if ... then ... elif ... Constructif statement is the
if ... then ... elif ...
if COMMAND
then
COMMAND_1
COMMAND_2
...
elif ANOTHER_COMMAND
then
COMMAND_A
COMMAND_B
...
...
[else
COMMAND_N1
COMMAND_N2
...]
fielif stands for "else if"elif must be followed by thenthen must either be on the next line ...else statement must be terminated by a fielif only requires a single fi at the endelif clauses as you wishelse clause is optional
Usage: PROGRAM_NAME ARG1 ARG2 ...
basename ...
if [ $# -gt 0 ]
then
dir=$1
else
echo Usage: $(basename $0) DIR_NAME
exit 1
fi
$ 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
...
donedo 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 Loopfor loops above are very different ...for loop
for loops ...for loops we saw above ...in ...for loop in Bash
for (( CMD_1; CMD_2; CMD_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 loopscontinuedo and donecontinue 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 loopbreakfor 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
svn update command
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