case
Statementselect
Statementread
Commandread
Commandcase
and for
in a Real World ScriptThe final exam will be held on Tuesday, May 14th from 3:00 - 6:00 PM.
The exam will be given in this room.
If for some reason you are not able to take the Final at the time it will be offered, you MUST send an email to me before the exam so we can make alternative arrangements.
60% of the points on this exam will consist of questions from the Ungraded Class Quizzes.
You do not need to study a Class Quiz question if the topic is not mentioned in either the Midterm or Final review.
The other questions I will make up specifically for this exam.
For these questions you will have to know
The last class on Thursday, May 2nd, will be a review session.
You will only be responsible for the material in that review session, which you will find here, and the review for the Midterm, 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.
The final is a closed book exam.
To prevent cheating, certain rules will be enforced during the exam.
Remember, the Midterm and Final determine 50% of your grade.
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.
This will be the last quiz.
Are there any questions before I begin?
for ... in ...
Loopsfor ... in
loop
for LOOP_VARIABLE in LIST_OF_VALUES
do
COMMAND_1
COMMAND_2
...
done
do
keyword
must be on a different line from the for
keyword ...
do
do
keyworddone
keyworddo
keyword is like the then
keyword in an if
statementfor ... 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 has a simpler structure than the for ... in ...
loop
for LOOP_VARIABLE
do
COMMAND_1
COMMAND_2
...
done
for
loops ...for ... in ...
loop gets its values ...in
for
loop gets its values from the command linefor
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 are totally different ...for
loops in programming languagesfor
statement
for
statements in programming languages ...for
loops we just have studied ...for
loop in Bashfor
loop in programming languages
for (( STMT_1; STMT_2; STMT_3 ))
do
COMMAND_1
COMMAND_2
...
done
for
keyword ...while
Loopsfor
loops run until all their values have
been used
while
loop works differentlywhile
...while
loops have the form
while COMMAND
do
COMMAND_1
COMMAND_2
...
done
until
Loopsuntil
loop is similar the while
loopuntil
loop runs ..until
...while
loopuntil
loop has the form
until COMMAND
do
COMMAND_1
COMMAND_2
...
done
while
loop is used much more often
than the until
loop
continue
do
and done
continue
causes the shell to stop running the rest of
the loop code
continue
does not cause the script to break out of the loopbreak
for ... in
and simple for
loops ...while
, until
and three statement
for
loops things are different
break
break
keyword case
Statementif ... then ... elif ...
statement
case
statement
case $TEST_VARIABLE in
PATTERN_1)
COMMAND_1A
COMMAND_1B
COMMAND_1C
...
;;
PATTERN_2)
COMMAND_2A
COMMAND_2B
COMMAND_2C
...
;;
PATTERN_3)
COMMAND_3A
COMMAND_3B
COMMAND_3C
...
;;
...
esac
case
statement it
case
statementesac
marks the end of the case
statementesac
is case
spelled backwards
$ cat case_1.sh
#! /bin/bash
#
# demonstrates how the case statement works
echo -n "Enter A, B, or C: "
read letter
case $letter in
A)
echo You entered A
;;
B)
echo You entered B
;;
C)
echo You entered C
;;
*)
echo You did not enter A, B, or C
;;
esac
echo Exiting program
$ ./case_1.sh
Enter A, B, or C: A
You entered A
Exiting program
$ ./case_1.sh
Enter A, B, or C: B
You entered B
Exiting program
$ ./case_1.sh
Enter A, B, or C: d
You did not enter A, B, or C
Exiting program
case
statement* | Matches any string of characters |
---|---|
? | Matches any single character |
[ ] | Every character within the brackets can match a single character in the test string |
| | Logical OR separates alternative patterns |
$ cat case_2.sh #! /bin/bash # # demonstrates the use of the | (logical or) # operator in patterns within a case statement echo -n "Enter A, B, or C: " read letter case $letter in a|A) echo You entered A ;; b|B) echo You entered B ;; c|C) echo You entered C ;; *) echo You did not enter A, B, or C ;; esac echo Exiting program $ ./case_2.sh Enter A, B, or C: A You entered A Exiting program $ ./case_2.sh Enter A, B, or C: a You entered A Exiting program
select
Statementselect
statement is a special kind of loop ...select
statement has following form
select LOOP_VARIABLE [in LIST_OF_VALUES]
do
COMMAND_1
COMMAND_2
COMMAND_3
...
done
in LIST_OF_VALUES
is optional
select
statement needs a list of valuesin
for
loopselect
statement it
do
and done
with that valueselect
construct is a loop
$ cat select_1.sh
#! /bin/bash
#
# demonstrates how the select statement works
PS3="Choose your fruit: "
select fruit in apple banana blueberry orange
do
echo You chose $fruit
echo That is choice number $REPLY
done
$ ./select_1.sh
1) apple
2) banana
3) blueberry
4) orange
Choose your fruit: 1
You chose apple
That is choice number 1
Choose your fruit: 2
You chose banana
That is choice number 2
Choose your fruit: 3
You chose blueberry
That is choice number 3
Choose your fruit: 4
You chose orange
That is choice number 4
Choose your fruit: ^C
$ cat select_2.sh #! /bin/bash # # demonstrates how the select structure works # taking argument from the command line PS3="Choose your fruit: " select fruit do echo You chose $fruit echo That is choice number $REPLY done $ ./select_2.sh peaches pears watermelons 1) peaches 2) pears 3) watermelons Choose your fruit: 1 You chose peaches That is choice number 1 Choose your fruit: 2 You chose pears That is choice number 2 Choose your fruit: 3 You chose watermelons That is choice number 3 Choose your fruit: ^C
select
statement uses a number of
keyword shell variables
$ cat select_3.sh #! /bin/bash # # demonstrates the select statement where # PS3 has the default value select fruit in apple banana blueberry orange do echo You chose $fruit echo That is choice number $REPLY done $ ./select_3.sh 1) apple 2) banana 3) blueberry 4) orange #? 3 You chose blueberry That is choice number 3 #? 4 You chose orange That is choice number 4 #? ^C
select
is
REPLY
select
statement will not stop on its own$ cat select_4.sh #! /bin/bash # # demonstrates a select menu with a stop value PS3="Choose your fruit: " select fruit in apple banana blueberry orange STOP do if [ $fruit = STOP ] then echo About to leave break fi echo You chose $fruit echo That is choice number $REPLY done echo Exiting program $ ./select_4.sh 1) apple 2) banana 3) blueberry 4) orange 5) STOP Choose your fruit: 2 You chose banana That is choice number 2 Choose your fruit: 5 About to leave Exiting program
break
to jump out of the loopselect
in a script
PS3="Select the operation: " select opt in add subtract multiply divide quit; do case $opt in add) read -p "Enter the first number: " n1 read -p "Enter the second number: " n2 echo "$n1 + $n2 = $(($n1+$n2))" ;; subtract) read -p "Enter the first number: " n1 read -p "Enter the second number: " n2 echo "$n1 - $n2 = $(($n1-$n2))" ;; multiply) read -p "Enter the first number: " n1 read -p "Enter the second number: " n2 echo "$n1 * $n2 = $(($n1*$n2))" ;; divide) read -p "Enter the first number: " n1 read -p "Enter the second number: " n2 echo "$n1 / $n2 = $(($n1/$n2))" ;; quit) break ;; *) echo "Invalid option $REPLY"
1) add 2) subtract 3) multiply 4) divide 5) quit Select the operation: 1 Enter the first number: 4 Enter the second number: 5 4 + 5 = 9 Select the operation: 2 Enter the first number: 4 Enter the second number: 5 4 - 5 = -1 Select the operation: 9 Invalid option 9 Select the operation: 5
select
statement will not be on the Finalread
Commandread
commandread
command it
read
command
$ cat read_1.sh
#! /bin/bash
#
# demonstrate use of the read command
echo -n "Please enter a word: "
read reply
echo You entered: $reply
$ ./read_1.sh
Please enter a word: foo
You entered: foo
read
takes a value from the terminal$ ./read_1.sh Please enter a word: foo bar bletch You entered: foo bar bletch
echo
to print a prompt for the userread
is called with the -p option ...read
will print the string as a prompt
$ cat read_2.sh
#! /bin/bash
#
# demonstrate use of the read command using the prompt option
read -p "Please enter a word: " reply
echo You entered: $reply
$ ./read_2.sh
Please enter a word: foo
You entered: foo
read
does not use the Readline Library ...read with
the -e option ...$ ./read_2.sh Please enter a word: foooo^[[D^[[D You entered: foooo
read
command ignores thisread
Commandread
command with the -p optionread -p Next?
$ cat here.sh
#! /bin/bash
#
# demonstrates how here documents work
read -p "Please enter a city: " city
grep $city <<EOF
Boston Red Sox
New York Yankees
Toronto Blue Jays
Baltimore Orioles
Tampa Bay Rays
EOF
$ ./here.sh
Please enter a city: Boston
Boston Red Sox
echo
commandscat
and a here document
$ cat here_instructions.sh #! /bin/bash cat <<EOF This is a silly script to show you how to use a here document to display instructions for using a script --------------------------------------------- Hit Enter when prompted EOF read -p "Done? " echo Goodbye $ ./here_instructions.sh This is a silly script to show you how to use a here document to display instructions for using a script --------------------------------------------- Hit Enter when prompted Done? Goodbye
case
and for
in a Real World Scriptcase
and for
constructs$ cat -n homework_collect.sh 1 #! /bin/bash 2 # 3 # creates a new homework a directory for 4 # each student and copies the relevant files 5 # created by that student for the current 6 # homework assignment into it. 7 8 if [ $# -lt 3 ] 9 then 10 echo Usage: $(basename $0) " COURSE_ID HOMEWORK_DIRECTORY EXTENSION" 11 exit 12 fi 13 14 course_id=$1 15 homework_dir=$2 16 ext=$3 17 18 case $course_id in 19 it116) 20 course_dir=$s1cd 21 student_ids=$s1cl 22 ;; 23 it244) 24 course_dir=$lncd 25 student_ids=$lncl 26 ;; 27 *) 28 echo $course_id is not a valid course ID 29 exit 30 esac 31 32 case $ext in 33 txt|sh|py|'*') 34 ;; 35 *) 36 echo $ext is not a valid extension 37 exit 38 esac 39 40 homework_no=$($bin/python/two_digits_from_string.py $homework_dir) 41 if [ $? != 0 ] 42 then 43 echo $homework_dir is not a valid homework directory 44 exit 45 fi 46 47 error_file=${homework_no}_homework_submissions_missing.txt 48 49 50 echo $course_id 51 52 echo 53 54 if [ -e $error_file ] 55 then 56 rm $error_file 57 fi 58 59 for unix_id in $student_ids 60 do 61 echo $unix_id 62 if [ ! -d $unix_id ] 63 then 64 mkdir $unix_id 65 fi 66 cd $unix_id 67 cp $course_dir/$unix_id/hw/$homework_dir/*.$ext . 2>> ../$error_file 68 cd .. 69 done