1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

55
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell

Transcript of 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

Page 1: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

1© 2001 John Urrutia. All rights reserved.

Chapter 10

using the Bourne Again

Shell

Page 2: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

2© 2001 John Urrutia. All rights reserved.

TopicsControl Structures

The Here Document

Expanding NULL or unset variables

The Builtins

Functions

Page 3: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

3© 2001 John Urrutia. All rights reserved.

Control StructuresSelections

if … then … fi One-Way Selection

if … then … else … fi Two-Way Selection

if … then … elif … fi Multi -Way Selection

The above builtins must be on separate

lines or separated by ;

Page 4: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

4© 2001 John Urrutia. All rights reserved.

Control StructuresSyntax:

if test expression or [expression ]

thencommand(s)

fi

Must Evaluate True or False

Boolean Expression

Executes if

expression is true

test expression or [expression ]

if

then

Executes if

expression is false

fi

Page 5: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

5© 2001 John Urrutia. All rights reserved.

Boolean ExpressionsAlgebra created by George Boole

Always evaluates to a binary stateGenerally:

1 is TRUE0 is FALSE

Page 6: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

6© 2001 John Urrutia. All rights reserved.

Boolean or Logical Operators

And-a

Or-o

Not !

Page 7: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

7© 2001 John Urrutia. All rights reserved.

and Boolean Truth Tables

Expr 1 Expr 2 -a

True True True

FalseTrue False

False True False

False False False

Page 8: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

8© 2001 John Urrutia. All rights reserved.

or Boolean Truth Tables

True True True

FalseTrue True

False True True

False False False

Expr 1 Expr 2 -o

Page 9: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

9© 2001 John Urrutia. All rights reserved.

not Boolean Truth Tables

Expr !

FalseTrue

False True

Page 10: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

10© 2001 John Urrutia. All rights reserved.

test ing, test ing, test ingtest expression or [expression ]

Evaluates the expression and returns a Boolean true or false.

expression can be simple or compound

Criteria:String IntegerFilenameFile descriptor number

Page 11: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

11© 2001 John Urrutia. All rights reserved.

Test CriteriaString expressions – applies to string

variables or literalsIs null or

Length is zero (-z) orLength is > 0 (-n)

string1 = or != string2

If you are comparing literals quote them

Page 12: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

12© 2001 John Urrutia. All rights reserved.

Testing Strings

…]$ if test "not" != "equal";then echo Not Equal;fiNot Equal…]$ if [ "not" = "equal“ ];then echo Equal;fi…]$

Page 13: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

13© 2001 John Urrutia. All rights reserved.

Test Criteria (cont.)Integer relationship

-gt greater than

-ge greater than or equal to

-eq equal to

-ne not equal to

-le less than or equal to

-lt less than

Page 14: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

14© 2001 John Urrutia. All rights reserved.

Testing Integers

…]$ if test 123 -ne 234;then echo Not Equal;fiNot Equal…]$ if [ 123 -eq 234 ];then echo Equal;fi…]$

Page 15: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

15© 2001 John Urrutia. All rights reserved.

Test Criteria (cont.)Filename expression – file exists and has

specific attributesdrewsx

-directory

-readable

-exists

-writable

-size – file has data (length >0)

-x executable

Page 16: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

16© 2001 John Urrutia. All rights reserved.

Testing Files

…]$ if [ ! -e “TestFile” ];then echo Not found;fiNot found…]$

…]$ if test -e “TestFile”;then echo Found it;fi…]$

Page 17: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

17© 2001 John Urrutia. All rights reserved.

Bourne - if, thenif

Establishes a control structureFollowed by a test command

thenCommands executed if test is true

Page 18: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

18© 2001 John Urrutia. All rights reserved.

Bourne – else, fielse

Commands executed if test is false

fiTerminates this if statement

Page 19: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

19© 2001 John Urrutia. All rights reserved.

if …then … else … fi

Start Test 1 False

True

Page 20: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

20© 2001 John Urrutia. All rights reserved.

Bourne - elifelif

“else if” structureLinear in natureSimilar to the case or switch in selection

but completely different in execution.

Page 21: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

21© 2001 John Urrutia. All rights reserved.

if …then … elif … else … fi

Start Test 2 False

True

Test 1

True

if

then

elif else

Page 22: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

22© 2001 John Urrutia. All rights reserved.

if … then … [ else or elif ] … fi

2 structuresif […] then cmds

if […] then cmds else cmds else cmds fi

if […] then cmds elif […] then cmds else cmds fi

Page 23: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

23© 2001 John Urrutia. All rights reserved.

if [ $a –gt 2 ]then

if [ $a –gt 3 ]then

echo value is 4 else

echo value is 3fi

elseif [ $a –gt 1 ]then

echo value is 2else

echo value is 1fi

if [ $a –gt 3 ]then

echo value is 4

elif [ $a –gt 2 ]then

echo value is 3

elif [ $a –gt 1 ] then

echo value is 2

elseecho value is 1

fi

Page 24: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

24© 2001 John Urrutia. All rights reserved.

Control StructuresIterations

for … in

for

Page 25: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

25© 2001 John Urrutia. All rights reserved.

for… and for …inAssign

element

Docommands

Moreelements

Done

Page 26: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

26© 2001 John Urrutia. All rights reserved.

Here we go loop-de-loopfor x in a b c

do cmd

cmd cmd done

Substitutes the x variable with each in variable and executes the commands between do and done

Page 27: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

27© 2001 John Urrutia. All rights reserved.

California the land of …echo “California the land of – “

for info in fruits nuts cheese wine

do echo –n “ $info”

done

echo –e “\n And of course Hollywood”

Page 28: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

28© 2001 John Urrutia. All rights reserved.

Here we go loop-de-loopfor x

docmdcmdcmd

done

Substitutes the x with command line arguments and executes the commands between do and done

Page 29: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

29© 2001 John Urrutia. All rights reserved.

California the land of …echo “California the land of – “

for arg in fruits nuts cheese wine

doecho –n “ $arg”

done

echo “ And of course Hollywood”

This modification causes the scriptto display all command line arguments

again

Page 30: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

30© 2001 John Urrutia. All rights reserved.

Control StructuresIterations

while

until

Page 31: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

31© 2001 John Urrutia. All rights reserved.

While I’m here – Until it’s gonewhile […] (initial state true - go)

until […] (initial state true – stop)do

cmd cmd cmddone

Page 32: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

32© 2001 John Urrutia. All rights reserved.

While you were out… cold

WhileTest

docommands

Done

Pre-Test Loop

Page 33: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

33© 2001 John Urrutia. All rights reserved.

Until … The cows come home

untilTest

docommands

Done

Pre-Test Loop

Page 34: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

34© 2001 John Urrutia. All rights reserved.

Continue to Breakcontinue and break

Used in for while and until structuresbreak – terminates the structurecontinue – terminates this iteration

Page 35: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

35© 2001 John Urrutia. All rights reserved.

continuefor idx in 1 2 3 4 5 6 7 8 9 10do

if [ $idx –le 3 ] ; thenecho “continue”continue

fiecho $idx

done

Continues three times

Echoes 4 – 10

Page 36: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

36© 2001 John Urrutia. All rights reserved.

breakfor idx in 1 2 3 4 5 6 7 8 9 10do

if [ $idx –gt 6 ] ; thenecho “break”break

fiecho $idx

done

Stops for loop after

sixth iteration

Echoes 1 – 6

Page 37: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

37© 2001 John Urrutia. All rights reserved.

Bourne – caseThe case command tests for multiple

values in a variable

Allows the use of “wild cards”

First match wins

Page 38: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

38© 2001 John Urrutia. All rights reserved.

Get on this case

case $var in test-var1 )

cmd1;cmd2cmd3;;

test-var2 )cmds;;

) cmds ;;esac

Page 39: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

39© 2001 John Urrutia. All rights reserved.

case “$variable” inA|a )

echo Entered A or a;;

[0-9] )echo Entered number;;

?z* )echo Entered z in 2nd position;;

esac

Page 40: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

40© 2001 John Urrutia. All rights reserved.

TopicsControl Structures

The Here Document

Expanding NULL or unset variables

The Builtins

Functions

Page 41: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

41© 2001 John Urrutia. All rights reserved.

Here boy

<<

The Here DocumentAllows in-stream data to feed a

script.

Must start with << and a data delimiter character

Data delimiter character on line by itself - terminates

Page 42: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

42© 2001 John Urrutia. All rights reserved.

Here in the script

#!/bin/bashgrep –i “$1” <<+Alex June 22Babs February 3Leroy January 20+

echo “All Done now”

Page 43: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

43© 2001 John Urrutia. All rights reserved.

Bundle script #!/bin/bashecho “# To unbundle sh this filefor ido echo “echo $i 1>&2”

echo “cat >$i <<‘End of $i’”cat $iecho “End of $i”

done

Page 44: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

44© 2001 John Urrutia. All rights reserved.

Bundle script -- output # To unbundle sh this fileecho 1st filename 1>&2cat > 1st filename <<‘End of 1st filename’Contents of 1st filename..End of 1st filename

Page 45: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

45© 2001 John Urrutia. All rights reserved.

TopicsControl Structures

The Here Document

Expanding NULL or unset variables

The Builtins

Functions

Page 46: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

46© 2001 John Urrutia. All rights reserved.

Expanding Null or Unset Vars. ${name} – expands to the value of

the variable.

If variable is null or not set the expansion produces a null string (\0)

${name:-default} – expands to the value of the variable or the default value if null.

Page 47: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

47© 2001 John Urrutia. All rights reserved.

Expanding Null or Unset Vars. ${name:=default} – expands to

the value of the variable or the default value if null and sets the variable to the default value.

: ${name:=default} – if null sets the variable to the default value.

${name:?message} – if null or unset sends message to stdout.

Page 48: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

48© 2001 John Urrutia. All rights reserved.

TopicsControl Structures

The Here Document

Expanding NULL or unset variables

The Builtins

Functions

Page 49: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

49© 2001 John Urrutia. All rights reserved.

The execute commandThe exec command

Executes scripts or programs

Runs under the same PID

Provides access to the original environment variables

Terminates current process.

Page 50: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

50© 2001 John Urrutia. All rights reserved.

The execute commandThe exec command

Can be used to redirect stdin, stdout and stderr from inside a script.

exec < infile

exec > outfile 2> errfile

Page 51: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

51© 2001 John Urrutia. All rights reserved.

I feel like trap – let’s kill a PIDtrap ’action ’ [signal number]

64 signals available 0 – 631 – Phone Hangup2 – Pressing an interrupt key ( ^c)3 – Pressing the quit key (Ctrl+Shft+| or \)9 – Kill command (without mercy ^d)15 – Terminate process (kill command)20 – Job control STOP key (^z)

Page 52: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

52© 2001 John Urrutia. All rights reserved.

I feel like trap – let’s kill a PIDkill - [signal number] [pid ]

Sends a signal to one or more PIDs

By default sends signal 15 (SIGTERM)

Using signal 9 is un-trappable

Get-out … NOW!kill –9 0

Page 53: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

53© 2001 John Urrutia. All rights reserved.

TopicsControl Structures

The Here Document

Expanding NULL or unset variables

The Builtins

Functions

Page 54: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

54© 2001 John Urrutia. All rights reserved.

Play that Function musicFunctions are like pre-processed

scripts.

Can be stored in .profile or in script files.

Generally used by more than 1 script.

Page 55: 1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.

55© 2001 John Urrutia. All rights reserved.

Function declarationFunction-name ()

{commands

}

Functions can be called with arguments and process them the same way a command line would.