26
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

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

Embed Size (px)

Citation preview

Page 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

Xuan Guo CSC 3320 1

Chapter 5The 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

Page 2: 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

Xuan Guo CSC 3320 2

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

mweeks$ x="one two"mweeks$ echo $xone twomweeks$ echo ${x}3one two3

Page 3: 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

Xuan Guo CSC 3320 3

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” ${name-word}

$ echo ${x-three}one two

$ echo ${z-three}three

Page 4: 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

Xuan Guo CSC 3320 4

Access Variations

• If name is set, replace with word • Variable x is set• Variable z is not set, so nothing is

printed${name+word}

$ echo ${x+three}three

$ echo ${z+three}

Page 5: 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

Xuan Guo CSC 3320 5

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”${name=word}

$ echo ${x=three}one two$ echo ${z=three}three$ echo $zthree

Page 6: 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

Xuan Guo CSC 3320 6

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 ${name?word}

$ echo ${x?three}one two

$ echo ${w?three}-bash: w: three

Page 7: 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

Xuan Guo CSC 3320 7

Reading from Standard Input

• read [-p] {variable}+• Reads 1 line• Examples

$ read v1 v2one two $ echo $v1one$ echo $v2two

$ read v1 v2 one two three four$ echo $v1one$ echo $v2two three four

Page 8: 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

Xuan Guo CSC 3320 8

Example Reading Multiple Lines

$ cat readme.sh #!/bin/bash# read multiple lines

read v1read v2echo "you said $v1"echo "then you said $v2"

$ ./readme.sh one twothree four fiveyou said one twothen you said three four five

Page 9: 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

Xuan Guo CSC 3320 9

Exporting Variables

• export [name[=value]]• Makes variables available in

environment• e.g. export x

$ v1="one two"$ export v1$ shsh-3.1$ echo $v1one twosh-3.1$

Page 10: 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

Xuan Guo CSC 3320 10

Predefined Locals

$ cat predefined.shecho You passed $# parameters.echo These are: "$@"echo process ID of last background process = $!echo process ID of this shell = $$notAcommandecho Last command returned with $? as the status.$ ./predefined.sh one two three fourYou passed 4 parameters.These are: one two three fourprocess ID of last background process =process ID of this shell = 21712./predefined.sh: line 7: notAcommand: command not foundLast command returned with 127 as the status.

Page 11: 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

Xuan Guo CSC 3320 11

Arithmetic

• $ expr expression• Bourne shell does not directly do math• Command expr evaluates expressions

– Supports • Multiplication (\*), Division (/), Remainder

(%)• Add (+), Subtract (-)• Equal (=), Not Equal (!=)• Less (\<), Less/Eq (\<=), Greater (\>),

Greater/Eq (\>=)• And (\&), Or (\|)

Page 12: 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

Xuan Guo CSC 3320 12

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

Page 13: 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

Xuan Guo CSC 3320 13

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

Page 14: 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

Xuan Guo CSC 3320 14

Case Structure

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

Page 15: 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

Xuan Guo CSC 3320 15

Case Structure Example

$ cat testcase.sh

echo "Type out the word for 1 or 2:"read v1case $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:twoYou entered 2$ ./testcase.sh Type out the word for 1 or 2:TwoYou entered 2$ ./testcase.sh Type out the word for 1 or 2:threesorry

Page 16: 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

Xuan Guo CSC 3320 16

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

for name in {word}*do

command listdone

Page 17: 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

Xuan Guo CSC 3320 17

For Loop Example

$ cat testfor.sh

params=$@for value in $paramsdo echo param: $valuedone

$ ./testfor.sh one two treeparam: oneparam: twoparam: tree

Page 18: 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

Xuan Guo CSC 3320 18

If .. Then

if [ expression ]then Statement(s) to be executed if expression is truefi

if [ expression ]then Statement(s) to be executed if expression is trueelse Statement(s) to be executed if expression is not truefi

Page 19: 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

Xuan Guo CSC 3320 19

If .. Then

if [ expression 1 ]then Statement(s) to be executed if expression 1 is trueelif [ expression 2 ]then Statement(s) to be executed if expression 2 is trueelif [ expression 3 ]then Statement(s) to be executed if expression 3 is trueelse Statement(s) to be executed if no expression is truefi

Page 20: 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

Xuan Guo CSC 3320 20

If .. Then Example

$ cat testif.sh echo "enter a word: "read v1if [ -e $v1 ]then echo "A file by that name exists."else echo "No file by that name exists."fi

$ ./testif.sh enter a word: dummyA file by that name exists.$ ./testif.sh enter a word: twoNo file by that name exists.

Page 21: 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

Xuan Guo CSC 3320 21

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

Page 22: 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

Xuan Guo CSC 3320 22

Until .. do .. done

• End loop: break• Go to next iteration: continue

until commanddo Statement(s) to be executed until command is truedone

Page 23: 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

Xuan Guo CSC 3320 23

Until .. do .. done Example

$ cat testuntil.sh

x=1until [ $x -gt 3 ]do

echo x = $xx=`expr $x + 1`

done

$ ./testuntil.sh x = 1x = 2x = 3

Page 24: 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

Xuan Guo CSC 3320 24

While Loop

• End loop: break• Go to next iteration: continue

while commanddo Statement(s) to be executed if command is truedone

Page 25: 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

Xuan Guo CSC 3320 25

While Loop Example

$ cat testwhile.sh

x=1while [ $x -lt 4 ]do

echo x = ${x}, less than four x=`expr $x + 1`

done

$ ./testwhile.sh x = 1, less than fourx = 2, less than fourx = 3, less than four

Page 26: 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

Xuan Guo CSC 3320 26

Review

• Variable assignment and access• Reading standard input• Arithmetic and pattern matching

(expr)• Control structures (case, for, if,

while, until)