test in Scriptsif ... then ... else ... Constructif ... then ... elif ... Constructfor ... in ... Loopsfor Loopsfor Loopswhile Loopsuntil Loopscontinuebreakfor Loop ExamplesI have posted homework 11 here.
It is due this coming Sunday at 11:59 PM.
This is the last homework assignment.
The final exam will be given on Tuesday, December 19th from 3:00 - 6:00.
The exam will be given in McCormack 3-430.
There will be 50 questions, which will be similar to the questions on the mid-term and the quizzes.
As with the Mid-term, 60% of the questions will come from the ungraded Class Quizzes.
The questions will cover all of the material for this course.
The last class, Tuesday, December 12th, will be a review session.
You will only be responsible for the material contained in the Class Notes for that class, and the Class Notes for the review session for the Mid-term, which you will find here.
Although the time alloted for the exam is 3 hours, I would expect that most of you would not need that much time.
You will not be able to leave the room until you turn in your exam paper so you should visit the restroom before you take the test.
The final is a closed book exam.
test in Scriptstest ...
test $number1 -gt $number2
or
[ $number1 -gt $number2 ]test returns true or false through the exit statustest returns an exit status of 0 ...test returns an exit status of 1
Usage: PROGRAM_NAME ARG1 ARG2 ...
basename command ...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
...
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 endbash with the -x option ...
$ 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
bash prints a line from the script ...for ... in ... Loopsif statement ...for ... 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 ...for ... 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.
forfor Loopsfor loop ...for ... in ... loop
for LOOP_VARIABLE
do
COMMAND_1
COMMAND_2
...
done
for loops ...for ... in ... loop gets values ...in for loop is runfor loop gets its values from the command linefor loop ...
$ 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 different ...for statement
for statements in programming lanugages ...for loops above ...for ... in ... statement ...in ...for statement ...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 continue running ...while loops have the form
while COMMAND
do
COMMAND_1
COMMAND_2
...
done
do and done ...
$ 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
$ ./count_to_nine.sh
0123456789
echo -n ...echo command in the while loop uses the -n option ...echo commandwhile loop tests ...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 ...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 ...until loopcontinuedo 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 ...breakfor ... in and simple for loops ...while, until, and three statement for loops ...breakbreak ...
$ 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
for ... in loopfor Loop Examplesfor loop often ...assignment_collect.sh sh
for loop ...$ for id in $lncl > do > rm $id/work.sh > done
$ for id in $lncl > do > echo $id > done adelasse ali andre001 ashish1 ...
cat -n upsvn.sh
1 pushd $wksp &> /dev/null
2 for entry in *
3 do
4 if [ -d $entry ]
5 then
6 echo $entry
7 cd $entry
8 svn update
9 cd ..
10 fi
11 done
12 echo notes
13 cd $notes
14 svn update
15 popd &> /dev/null
for loop using the variable entry if statement ...