shift
- Promotes Command Line Argumentsset
- Initialize Command Line Argumentsset
The final exam will be held on Tuesday, December 17th from 11:30 AM - 2:30 PM.
The exam will NOT be given in this room.
Instead it will be given in University Hall Y02-2330, one floor below.
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 Weekly Graded Quizzes.
You do not need to study a Graded Quiz question if the topic is not mentioned in either the Midterm or Final review.
There is a link to the answers to the graded quizze on the class web page.
The other questions I will make up specifically for this exam.
For these questions you will have to know
The last class on Tuesday, December 10th, 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.
At the end of each semester we offer you the opportunity to say what you think about this course.
What have I done right?
What have I done wrong?
What can I do better?
You are not asked for you name.
So the submissions are anonymous.
I will not see your responses until after I have submitted grades for this course.
We collect this feedback through Course Evaluations.
I will use what you say to make this course better.
To complete the course evaluation, use the following link .
You have until Friday, December 20th, to submit the evaluation.
The new topics we discuss today with not be on the final exam.
The new IT Club will hold it's first event, this coming Thursday, December 5th at 3 PM in the Integrated Sciences Complex, Room 3300.
Jason Agress wil speak about his experiences in the IT industry.
This is a great chance to meet other IT students and expand your network.
Holiday food will be served.
You will find a poster for the event here.
Are there any questions before I begin?
case
Statementif ... elif
statementcase
statement is easier to usecase
statement 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 structure 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
* | 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
read
Commandread
command sets a variable to a value entered by the userread
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
assigns everything typed on the command line ...
$ ./read_1.sh
Please enter a word:
You entered: foo bar bletch
echo
to print a promptread
will print a prompt if I use the -p option
$ 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 does not allow you to edit text on the command lineread
$ 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
$ city=Boston
$ echo "I work in $city Massachusetts"
I work in Boston Massachusetts
$ filename=test $ echo Creating $filename.txt Creating test.txt $ dirname=test_dir $ echo Creating $dirname/test.txt Creating test_dir/test.txt
$ echo Creating $filename_1.txt
Creating .txt
$ echo Creating ${filename}_1.txt Creating test_1.txt
for homework_no in 01 02 03 04 05 06 07 08 09 10 11 do scores_list=../../submitted_it244/homework_submitted_it244/${homework_no}_homework_submitted_it244/${homework_no}_homework_scores_it244.txt if [ -f $scores_list ] then echo homework_$homework_no cp $scores_list . else echo Cannot find $scores_list fi done
for
loop to assign values to the loop variable
homework_no
$ echo $$ 6834 $ ps PID TTY TIME CMD 6834 pts/1 00:00:00 bash 7024 pts/1 00:00:00 ps
$ sleep 60 & [1] 7347 $ echo $! 7347
$ ./bother.sh > /dev/null & [1] 5274 $ jobs [1]+ Running ./bother.sh > /dev/null & $ echo $! 5274 $ kill $! [1]+ Terminated ./bother.sh > /dev/null $ jobs $
$ pwd /home/it244gh/it244/work $ echo $? 0 $ ls asdfasd ls: cannot access asdfasd: No such file or directory $ echo $? 2
exit
built-in
$ cat exit.sh #! /bin/bash # # demonstrates the use of the exit command with a status code exit 2 $ ./exit.sh $ echo $? 2
$ cat arg_count.sh #!/bin/bash # # Prints the number of arguments given to this script echo This script received $# arguments $ ./arg_count.sh foo bar bletch This script received 3 arguments
$ cat command_name.sh #!/bin/bash # # prints the pathname by which called this script echo This script was called using the pathname $0 $ ./command_name.sh This script was called using the pathname ./command_name.sh $ /home/ghoffman/examples_it244/command_name.sh This script was called using the pathname /home/ghoffman/examples_it244/command_name.sh
basename
command ...$ basename /home/ghoffman/examples_it244/command_name.sh command_name.sh
$ cat print_positionals.sh #!/bin/bash # # Prints the value of the first four positional arguments echo 0: $0 echo 1: $1 echo 2: $2 echo 3: $3 $ ./print_positionals.sh foo bar bletch 0: ./print_positionals.sh 1: foo 2: bar 3: bletch
$ ./print_positionals.sh foo bar 0: ./print_positionals.sh 1: foo 2: bar 3:
test
on a positional parameter
$ cat greater_than_zero.sh
#! /bin/bash
#
# this script test whether its first command line argument is greater than 0
if [ $1 -gt 0 ]
then
echo $1 is greater than 0
else
echo $1 is not greater than 0
fi
$ ./greater_than_zero.sh
./greater_than_zero.sh: line 5: [: -gt: unary operator expected
is not greater than 0
$ cat param_test_1.sh #! /bin/bash echo '$#' $# echo '$1' $1 echo '$2' $2 echo '$3' $3 echo '$4' $4 echo '$5' $5 echo '$6' $6 echo '$7' $7 echo '$8' $8 echo '$9' $9 echo '$10' $10 echo '$11' $11 echo '$12' $12 echo '$13' $13 echo '$14' $14 echo '$15' $15 echo '$16' $16 echo '$17' $17 echo '$18' $18 echo '$19' $19 echo '$20' $20
$ ./param_test_1.sh a b c d e f g h i j k l m n o p q r s t $# 20 $1 a $2 b $3 c $4 d $5 e $6 f $7 g $8 h $9 i $10 a0 $11 a1 $12 a2 $13 a3 $14 a4 $15 a5 $16 a6 $17 a7 $18 a8 $19 a9 $20 b0
$ cat param_test_2.sh #! /bin/bash echo '$#' $# echo '$1' $1 echo '$2' $2 echo '$3' $3 echo '$4' $4 echo '$5' $5 echo '$6' $6 echo '$7' $7 echo '$8' $8 echo '$9' $9 echo '$10' ${10} echo '$11' ${11} echo '$12' ${12} echo '$13' ${13} echo '$14' ${14} echo '$15' ${15} echo '$16' ${16} echo '$17' ${17} echo '$18' ${18} echo '$19' ${19} echo '$20' ${20}
$ ./param_test_2.sh a b c d e f g h i j k l m n o p q r s t $# 20 $1 a $2 b $3 c $4 d $5 e $6 f $7 g $8 h $9 i $10 j $11 k $12 l $13 m $14 n $15 o $16 p $17 q $18 r $19 s $20 t
$ cat special_param_test_1.sh #! /bin/bash # # demonstrates some properties of the special # parameters $* and $@ echo 'Here is $*: ' $* echo 'Here is $@: ' $@ $ ./special_param_test_1.sh 1 2 3 4 5 Here is $*: 1 2 3 4 5 Here is $@: 1 2 3 4 5
for ... in
loops
$ cat special_param_test_2.sh #! /bin/bash # # demonstrates some properties of the special # parameters $* and $@ echo 'The $* loop' for arg in $* do echo $arg done echo 'The $@ loop' for arg in $@ do echo $arg done $ ./special_param_test_2.sh 1 2 3 4 5 The $* loop 1 2 3 4 5 The $@ loop 1 2 3 4 5
$ cat special_param_test_3.sh #! /bin/bash # # demonstrates some properties of the special # parameters $* and $@ echo 'The $* loop' for arg in "$*" do echo $arg done echo 'The $@ loop' for arg in "$@" do echo $arg done $ ./special_param_test_3.sh 1 2 3 4 5 The $* loop 1 2 3 4 5 The $@ loop 1 2 3 4 5
shift
- Promotes Command Line Argumentsshift
built-in promotes command line arguments$ cat shift_1.sh #! /bin/bash # # demonstrates the use of the shift command echo '$1: ' $1 echo '$2: ' $2 echo '$3: ' $3 echo '$4: ' $4 echo echo shifting arguments shift echo echo '$1: ' $1 echo '$2: ' $2 echo '$3: ' $3 echo '$4: ' $4 $ ./shift_1.sh foo bar bletch bling $1: foo $2: bar $3: bletch $4: bling shifting arguments $1: bar $2: bletch $3: bling $4
shift
is called ...shift
is called with a numeric argument ...$ cat shift_2.sh #! /bin/bash # # demonstrates the use of the shift command with an integer argument echo '$1: ' $1 echo '$2: ' $2 echo '$3: ' $3 echo '$4: ' $4 echo echo shifting arguments by 2 shift 2 echo echo '$1: ' $1 echo '$2: ' $2 echo '$3: ' $3 echo '$4: ' $4 $ ./shift_2.sh foo bar bletch bling $1: foo $2: bar $3: bletch $4: bling shifting arguments by 2 $1: bletch $2: bling $3: $4:
shift
is called ...shift
to write a script ...$ cat shift_3.sh #! /bin/bash # # demonstrates the use of the shift in a while loop while [ ! -z $1 ] do echo 'The value of $1 is ' $1 shift done $ ./shift_3.sh foo bar bletch bling blam The value of $1 is foo The value of $1 is bar The value of $1 is bletch The value of $1 is bling The value of $1 is blam
shift
keeps promoting arguments until there are no more lefttest
returns true if the length of what follows is zeroset
- Initialize Command Line Argumentsset
command can create positional arguments ...set
and follow it with a list of values ...set
will assign each of those values to a positional parameterset
...$ cat set.sh #! /bin/bash # # demonstrates using the set command to assign values # to positional parameters echo This script received $# arguments from the command line echo '$1: ' $1 echo '$2: ' $2 echo '$3: ' $3 echo '$4: ' $4 echo echo 'After set bloo blah blim blak' set bloo blah blim blak echo '$1: ' $1 echo '$2: ' $2 echo '$3: ' $3 echo '$4: ' $4 $ ./set.sh foo fie foe fum This script received 4 arguments from the command line $1: foo $2: fie $3: foe $4: fum After set bloo blah blim blak $1: bloo $2: blah $3: blim $4: blak
set
the positional parameters had different valuesset
is a built-in commandset
has a completely different behavior when called with the -o or +o optionsset
sets or unsets a shell optionset
set
is one of those commands I thought I would never needset
set
if [ $# -eq 0 ] # construct assignment filename then set hw${assgn_num}.$ext fi
for
loop ...in
...$ team='Celtics' $ echo "Go $team" Go Celtics
VARIABLE_NAME=(ELEMENT1 ELEMENT2 ... )
$ cities=(Boston Chicago Philadelphia Cleveland)
$ echo ${cities[0]} Boston $ echo ${cities[1]}
$ echo $cities
Boston
$ echo $cities[2]
Boston[2]
$ echo ${cities[*]} Boston Chicago Philadelphia Cleveland $ echo ${cities[@]} Boston Chicago Philadelphia Cleveland
declare
built-indeclare
is normally used to set the attributes of a variabledeclare
to list every variable ...declare
with a dash,
- ...
declare
with the -a option$ c1=("${cities[*]}") $ echo $c1 Boston Chicago Philadelphia Cleveland $ c2=("${cities[@]}") $ echo $c2 Boston $ declare -a ... declare -a c1="([0]="Boston Chicago Philadelphia Cleveland")' declare -a c2="([0]="Boston" [1]="Chicago" [2]="Philadelphia" [3]="Cleveland")' declare -a cities="([0]="Boston" [1]="Chicago" [2]="Philadelphia" [3]="Cleveland")'
for homework_no in 01 02 03 04 05 06 07 08 09 10 11
homework_nums=(01 02 03 04 05 06 07 08 09 10 11) for homework_no in ${homework_nums[@]}
$ echo ${cities[0]}
Boston
$ echo ${#cities[0]}
6
$ cities[3]=Akron $ echo ${cities[@]} Boston Chicago Philadelphia Akron