case Statementselect Statementread Commandread Commandcase and for in a Real World ScriptI 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.
for ... in ... Loopsfor ... in loop
for LOOP_VARIABLE in LIST_OF_VALUES
do
COMMAND_1
COMMAND_2
...
donedo keyword
must be on a different line from the for keyword ...
dodo keyword ...done keyworddo keyword is like the then keyword in an if statementfor ... in loop, Bash
do and donedo 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 keyword, ...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 keywordfor 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 statement
for statements in programming languages ...for loops we just have studied ...for loop in Bash
for (( STMT_1; STMT_2; STMT_3 ))
do
COMMAND_1
COMMAND_2
...
done
for keyword ...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 executeduntil Loopsuntil loop is similar the while loopuntil loop continues running ...while ...until runs ...while loopuntil loop has the form
until COMMAND
do
COMMAND_1
COMMAND_2
...
done
while loop is used much more often than the until loopcontinuedo and done ...continue causes the shell to stop running the rest of the code ...do and done keywordscontinue does not cause the script to break out of the loopbreakfor ... in and simple for loops, the code leaves the loop ...while, until and three statement for loops ...breakbreak keyword ...case Statementif ... then ... elif ... statementcase statement, which has the following format
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 statementesac
* 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 keyword ...for loop canselect statement it
do and done with that valueselect statement is a loop construct that will run forever ...
$ 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 REPLYselect 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 loopread 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 in 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 user ...read with the -p option ...read print the 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 command will not allow you to change the text ...read does not use the Readline Library ...
$ ./read_2.sh
Please enter a word: foooo^?^?^?
You entered: foooo
read command ignores thisreadread Commandread command with the -p option ...read -p Next?
for loop ...$ for id in $lncl > do > ./ex02.sh $id > finger $id | head -1 > read -p "Next? " > done
$ 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
case and for in a Real World Scriptcase and for statement can be useful ...
$ cat -n hw_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 hw_dir=$2
16 ext=$3
17
18 case $course_id in
19 it244)
20 course_dir=$lncd
21 student_ids=$lncl
22 ;;
23 it441)
24 course_dir=$nacd
25 student_ids=$nacl
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 hw_no=$($bin/python/two_digits_from_string.py $hw_dir)
41 if [ $? != 0 ]
42 then
43 echo $hw_dir is not a valid homework directory
44 exit
45 fi
46
47 error_file=${hw_no}_hw_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/$hw_dir/*.$ext . 2>> ../$error_file
68 cd ..
69 done