550 likes | 677 Views
Chapter 10. Shell. using the Bourne Again Shell. Programming. Topics. Control Structures The Here Document Expanding NULL or unset variables The Builtins Functions. Control Structures. Selections if … then … fi One-Way Selection
E N D
Chapter 10 Shell using the Bourne Again Shell Programming
Topics • Control Structures • The Here Document • Expanding NULL or unset variables • The Builtins • Functions
Control Structures • Selections • 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 ;
Control Structures Boolean Expression • Syntax: if test expressionor[expression ] then command(s) fi … if test expressionor[expression ] Must Evaluate True or False then Executes if expression is true fi Executes if expression is false
Boolean Expressions • Algebra created by George Boole • Always evaluates to a binary state • Generally: • 1 is TRUE • 0 is FALSE
Boolean or Logical Operators • And • -a • Or • -o • Not • !
and Boolean Truth Tables -a True True True False True False False True False False False False
or Boolean Truth Tables -o True True True True True False False True True False False False
not Boolean Truth Tables ! True False False True
test ing,test ing,testing • test expressionor[expression] • Evaluates the expressionand returns a Boolean true or false. • expressioncan be simple or compound • Criteria: • String • Integer • Filename • File descriptor number
Test Criteria • String expressions – applies to string variables or literals • Is null orLength is zero (-z) orLength is > 0 (-n) • string1 = or != string2 • If you are comparing literals quote them
Testing Strings …]$ if test "not" != "equal";then echo Not Equal;fi Not Equal …]$ if [ "not" = "equal“ ];then echo Equal;fi …]$
Test Criteria (cont.) • Integer relationship • -gtgreater than • -ge greater than or equal to • -eqequal to • -ne not equal to • -le less than or equal to • -lt less than
Testing Integers …]$ if test 123 -ne 234;then echo Not Equal;fi Not Equal …]$ if [ 123 -eq 234 ];then echo Equal;fi …]$
Test Criteria (cont.) • Filename expression – file exists and has specific attributes • drewsx • -directory • -readable • -exists • -writable • -size – file has data (length >0) • -x executable
Testing Files …]$ if [ ! -e “TestFile” ];then echo Not found;fi Not found …]$ …]$ if test -e “TestFile”;then echo Found it;fi …]$
Bourne - if, then • if • Establishes a control structure • Followed by a test command • then • Commands executed if test is true
Bourne – else, fi • else • Commands executed if test is false • fi • Terminates this if statement
if …then … else … fi Test 1 Start False True
Bourne - elif • elif • “else if” structure • Linear in nature • Similar to the case or switch in selection but completely different in execution.
if …then … elif … else … fi Test 1 Test 2 Start False if elif else True True then
if … then … [ else or elif ] … fi • 2 structures • if […] thencmdsif […] thencmdselsecmdselsecmdsfi • if […] thencmdselif[…] thencmdselsecmdsfi
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 else echo value is 1 fi if [ $a –gt 2 ] then if [ $a –gt 3 ] then echo value is 4 else echo value is 3 fi else if [ $a –gt 1 ] then echo value is 2 else echo value is 1 fi
Control Structures • Iterations • for … in • for
for… and for …in Assign element Do commands yes More elements no Done
Here we go loop-de-loop • for x ina b c • docmd cmd cmddone • Substitutes the xvariable with each in variable and executes the commands between do and done
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”
Here we go loop-de-loop • for x docmd cmd cmddone • Substitutes the x with command line arguments and executes the commands between do and done
California the land of … again echo “California the land of – “ for arg in fruits nuts cheese wine do echo –n “ $arg”done echo “ And of course Hollywood” This modification causes the script to display all command line arguments
Control Structures • Iterations • while • until
While I’m here – Until it’s gone • while […](initial state true - go) • until […](initial state true – stop) • docmd cmd cmddone
While you were out… cold Pre-Test Loop false While Test Done true do commands
Until … The cows come home Pre-Test Loop true until Test Done false do commands
Continue to Break • continue and break • Used in for while and until structures • break – terminates the structure • continue – terminates this iteration
continue for idx in 1 2 3 4 5 6 7 8 9 10do if [ $idx –le 3 ] ; then echo “continue” continue fi echo $idxdone Continues three times Echoes 4 – 10
break for idx in 1 2 3 4 5 6 7 8 9 10do if [ $idx –gt 6 ] ; then echo “break” break fi echo $idxdone Stops for loop after sixth iteration Echoes 1 – 6
Bourne – case • The case command tests for multiple values in a variable • Allows the use of “wild cards” • First match wins
Get on this case • case$varin test-var1) cmd1;cmd2 cmd3;;test-var2) cmds;;) cmds ;;esac
case “$variable” in A|a ) echo Entered A or a ;; [0-9] ) echo Entered number ;; ?z* ) echo Entered z in 2nd position ;; esac | <==(Alternative Choice) [ ... ] <==(Class Choices) ? * <==(Wild Cards)
Topics • Control Structures • The Here Document • Expanding NULL or unset variables • The Builtins • Functions
Here boy << • The Here Document • Allows in-stream data to feed a script. • Must start with << and a data delimiter character • Data delimiter character on line by itself - terminates
Here in the script #!/bin/bashgrep –i “$1” <<+Alex June 22Babs February 3Leroy January 20+ echo “All Done now”
Bundle script #!/bin/bashecho “# To unbundle sh this filefor ido echo “echo $i 1>&2” echo “cat >$i <<‘End of $i’” cat $i echo “End of $i” done
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
Topics • Control Structures • The Here Document • Expanding NULL or unset variables • The Builtins • Functions
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.
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.
Topics • Control Structures • The Here Document • Expanding NULL or unset variables • The Builtins • Functions
Theexecutecommand • The exec command • Executes scripts or programs • Runs under the same PID • Provides access to the original environment variables • Terminates current process.
Theexecutecommand • The exec command • Can be used to redirect stdin, stdout and stderr from inside a script. • exec < infile • exec > outfile 2> errfile