Download presentation
Presentation is loading. Please wait.
Published byLilian Grant Modified over 9 years ago
1
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, 2003. Notes by Michael Weeks Updated by Xuan Guo CSC 3320 1
2
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 3320 2 mweeks$ x="one two" mweeks$ echo $x one two mweeks$ echo ${x}3 one two3
3
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 3320 3 ${name-word} $ echo ${x-three} one two $ echo ${z-three} three
4
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 3320 4 ${name+word} $ echo ${x+three} three $ echo ${z+three}
5
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 3320 5 ${name=word} $ echo ${x=three} one two $ echo ${z=three} three $ echo $z three
6
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 3320 6 ${name?word} $ echo ${x?three} one two $ echo ${w?three} -bash: w: three
7
Xuan Guo Reading from Standard Input read [-p] {variable}+ Reads 1 line Examples CSC 3320 7 $ 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
8
Xuan Guo Example Reading Multiple Lines CSC 3320 8 $ 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
9
Xuan Guo Exporting Variables export [name[=value]] Makes variables available in environment e.g. export x CSC 3320 9 $ v1="one two" $ export v1 $ sh sh-3.1$ echo $v1 one two sh-3.1$
10
Xuan Guo Predefined Locals CSC 3320 10 $ 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 = 21712./predefined.sh: line 7: notAcommand: command not found Last command returned with 127 as the status.
11
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 3320 11
12
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 3320 12
13
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 3320 13
14
Xuan Guo Case Structure CSC 3320 14 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
15
Xuan Guo Case Structure Example CSC 3320 15 $ 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
16
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 3320 16 for name in {word}* do command list done
17
Xuan Guo For Loop Example CSC 3320 17 $ cat testfor.sh params=$@ for value in $params do echo param: $value done $./testfor.sh one two tree param: one param: two param: tree
18
Xuan Guo If.. Then CSC 3320 18 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
19
Xuan Guo If.. Then CSC 3320 19 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
20
Xuan Guo If.. Then Example CSC 3320 20 $ 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.
21
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 3320 21
22
Xuan Guo Until.. do.. done End loop: break Go to next iteration: continue CSC 3320 22 until command do Statement(s) to be executed until command is true done
23
Xuan Guo Until.. do.. done Example CSC 3320 23 $ 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
24
Xuan Guo While Loop End loop: break Go to next iteration: continue CSC 3320 24 while command do Statement(s) to be executed if command is true done
25
Xuan Guo While Loop Example CSC 3320 25 $ 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
26
Xuan Guo Review Variable assignment and access Reading standard input Arithmetic and pattern matching (expr) Control structures (case, for, if, while, until) CSC 3320 26
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.