1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
-
Upload
cordelia-fox -
Category
Documents
-
view
215 -
download
1
Transcript of 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
2© 2001 John Urrutia. All rights reserved.
TopicsControl Structures
The Here Document
Expanding NULL or unset variables
The Builtins
Functions
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 ;
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
5© 2001 John Urrutia. All rights reserved.
Boolean ExpressionsAlgebra created by George Boole
Always evaluates to a binary stateGenerally:
1 is TRUE0 is FALSE
6© 2001 John Urrutia. All rights reserved.
Boolean or Logical Operators
And-a
Or-o
Not !
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
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
9© 2001 John Urrutia. All rights reserved.
not Boolean Truth Tables
Expr !
FalseTrue
False True
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
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
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…]$
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
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…]$
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
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…]$
17© 2001 John Urrutia. All rights reserved.
Bourne - if, thenif
Establishes a control structureFollowed by a test command
thenCommands executed if test is true
18© 2001 John Urrutia. All rights reserved.
Bourne – else, fielse
Commands executed if test is false
fiTerminates this if statement
19© 2001 John Urrutia. All rights reserved.
if …then … else … fi
Start Test 1 False
True
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.
21© 2001 John Urrutia. All rights reserved.
if …then … elif … else … fi
Start Test 2 False
True
Test 1
True
if
then
elif else
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
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
24© 2001 John Urrutia. All rights reserved.
Control StructuresIterations
for … in
for
25© 2001 John Urrutia. All rights reserved.
for… and for …inAssign
element
Docommands
Moreelements
Done
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
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”
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
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
30© 2001 John Urrutia. All rights reserved.
Control StructuresIterations
while
until
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
32© 2001 John Urrutia. All rights reserved.
While you were out… cold
WhileTest
docommands
Done
Pre-Test Loop
33© 2001 John Urrutia. All rights reserved.
Until … The cows come home
untilTest
docommands
Done
Pre-Test Loop
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
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
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
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
38© 2001 John Urrutia. All rights reserved.
Get on this case
case $var in test-var1 )
cmd1;cmd2cmd3;;
test-var2 )cmds;;
) cmds ;;esac
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
40© 2001 John Urrutia. All rights reserved.
TopicsControl Structures
The Here Document
Expanding NULL or unset variables
The Builtins
Functions
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
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”
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
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
45© 2001 John Urrutia. All rights reserved.
TopicsControl Structures
The Here Document
Expanding NULL or unset variables
The Builtins
Functions
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.
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.
48© 2001 John Urrutia. All rights reserved.
TopicsControl Structures
The Here Document
Expanding NULL or unset variables
The Builtins
Functions
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.
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
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)
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
53© 2001 John Urrutia. All rights reserved.
TopicsControl Structures
The Here Document
Expanding NULL or unset variables
The Builtins
Functions
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.
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.