Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks Updated by Xuan Guo CSC
Xuan Guo Creating/Assigning a Variable Name=value –Variable created if it does not exist –Need quotes if value contains spaces –e.g. x=”one two” Access variable with $ in front –e.g., echo $x –Also, echo ${x}3 CSC mweeks$ x="one two" mweeks$ echo $x one two mweeks$ echo ${x}3 one two3
Xuan Guo Access Variations If name not set, replace with word Variable x is set, so we see it displayed Variable x is not set, so we see “three” CSC ${name-word} $ echo ${x-three} one two $ echo ${z-three} three
Xuan Guo Access Variations If name is set, replace with word Variable x is set Variable z is not set, so nothing is printed CSC ${name+word} $ echo ${x+three} three $ echo ${z+three}
Xuan Guo Access Variations If name not set, assign with word Variable x is set, and printed Variable z is not set, so now it is set to “three” CSC ${name=word} $ echo ${x=three} one two $ echo ${z=three} three $ echo $z three
Xuan Guo Access Variations If name is set, return it Variable x is set If name is not set, print word to standard error (and quit) Variable w is not set CSC ${name?word} $ echo ${x?three} one two $ echo ${w?three} -bash: w: three
Xuan Guo Reading from Standard Input read [-p] {variable}+ Reads 1 line Examples CSC $ read v1 v2 one two $ echo $v1 one $ echo $v2 two $ read v1 v2 one two three four $ echo $v1 one $ echo $v2 two three four
Xuan Guo Example Reading Multiple Lines CSC $ cat readme.sh #!/bin/bash # read multiple lines read v1 read v2 echo "you said $v1" echo "then you said $v2" $./readme.sh one two three four five you said one two then you said three four five
Xuan Guo Exporting Variables export [name[=value]] Makes variables available in environment e.g. export x CSC $ v1="one two" $ export v1 $ sh sh-3.1$ echo $v1 one two sh-3.1$
Xuan Guo Predefined Locals CSC $ cat predefined.sh echo You passed $# parameters. echo These are: echo process ID of last background process = $! echo process ID of this shell = $$ notAcommand echo Last command returned with $? as the status. $./predefined.sh one two three four You passed 4 parameters. These are: one two three four process ID of last background process = process ID of this shell = /predefined.sh: line 7: notAcommand: command not found Last command returned with 127 as the status.
Xuan Guo Arithmetic $ expr expression Bourne shell does not directly do math Command expr evaluates expressions –Supports Multiplication (\*), Division (/), Remainder (%) Add (+), Subtract (-) Equal (=), Not Equal (!=) Less (\ ), Greater/Eq (\>=) And (\&), Or (\|) CSC
Xuan Guo expr Command expr also evaluates expressions STRING : REGEXP Anchored pattern match of REGEXP in STRING match STRING REGEXP Same as STRING : REGEXP substr STRING POS LENGTH Substring of STRING, POS counted from 1 index STRING CHARS Index in STRING where any CHARS is found, or 0 length STRING Length of STRING CSC
Xuan Guo test Command $ test expression OR just expression –Returns 0 if true –Returns nonzero if false -e file True if file Exists. -f file True if file is a regular File. -r file True if file is readable. -w file True if file is writable. -x file True if file is executable. -d file True if file is a Directory. String1 = String2 True if the strings are equal. See page 193 for a more complete list CSC
Xuan Guo Case Structure CSC case word in pattern1) Statement(s) to be executed if pattern1 matches ;; pattern2) Statement(s) to be executed if pattern2 matches ;; pattern3) Statement(s) to be executed if pattern3 matches ;; … *) Statement(s) to be executed if no pattern matches ;; esac
Xuan Guo Case Structure Example CSC $ cat testcase.sh echo "Type out the word for 1 or 2:" read v1 case $v1 in [Oo]ne) echo "You entered 1" ;; [Tt]wo) echo "You entered 2" ;; *) echo "sorry“ exit 1 ;; esac $./testcase.sh Type out the word for 1 or 2: two You entered 2 $./testcase.sh Type out the word for 1 or 2: Two You entered 2 $./testcase.sh Type out the word for 1 or 2: three sorry
Xuan Guo For Loop Loop where name gets each value in word, in turn Uses if no word given End loop: break Go to next iteration: continue CSC for name in {word}* do command list done
Xuan Guo For Loop Example CSC $ cat testfor.sh for value in $params do echo param: $value done $./testfor.sh one two tree param: one param: two param: tree
Xuan Guo If.. Then CSC if [ expression ] then Statement(s) to be executed if expression is true fi if [ expression ] then Statement(s) to be executed if expression is true else Statement(s) to be executed if expression is not true fi
Xuan Guo If.. Then CSC if [ expression 1 ] then Statement(s) to be executed if expression 1 is true elif [ expression 2 ] then Statement(s) to be executed if expression 2 is true elif [ expression 3 ] then Statement(s) to be executed if expression 3 is true else Statement(s) to be executed if no expression is true fi
Xuan Guo If.. Then Example CSC $ cat testif.sh echo "enter a word: " read v1 if [ -e $v1 ] then echo "A file by that name exists." else echo "No file by that name exists." fi $./testif.sh enter a word: dummy A file by that name exists. $./testif.sh enter a word: two No file by that name exists.
Xuan Guo Responding to a Signal $ trap command {signal}+ Executes command when signal is received Example: $ trap 'echo CTRL-C; exit 1' 2 –When user types Control-C (signal 2) –Print “CTRL-C” –Exit with status 1 CSC
Xuan Guo Until.. do.. done End loop: break Go to next iteration: continue CSC until command do Statement(s) to be executed until command is true done
Xuan Guo Until.. do.. done Example CSC $ cat testuntil.sh x=1 until [ $x -gt 3 ] do echo x = $x x=`expr $x + 1` done $./testuntil.sh x = 1 x = 2 x = 3
Xuan Guo While Loop End loop: break Go to next iteration: continue CSC while command do Statement(s) to be executed if command is true done
Xuan Guo While Loop Example CSC $ cat testwhile.sh x=1 while [ $x -lt 4 ] do echo x = ${x}, less than four x=`expr $x + 1` done $./testwhile.sh x = 1, less than four x = 2, less than four x = 3, less than four
Xuan Guo Review Variable assignment and access Reading standard input Arithmetic and pattern matching (expr) Control structures (case, for, if, while, until) CSC