1 / 55

Chapter 10

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

joyce
Télécharger la présentation

Chapter 10

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 10 Shell using the Bourne Again Shell Programming

  2. Topics • Control Structures • The Here Document • Expanding NULL or unset variables • The Builtins • Functions

  3. 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 ;

  4. 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

  5. Boolean Expressions • Algebra created by George Boole • Always evaluates to a binary state • Generally: • 1 is TRUE • 0 is FALSE

  6. Boolean or Logical Operators • And • -a • Or • -o • Not • !

  7. and Boolean Truth Tables -a True True True False True False False True False False False False

  8. or Boolean Truth Tables -o True True True True True False False True True False False False

  9. not Boolean Truth Tables ! True False False True

  10. 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

  11. 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

  12. Testing Strings …]$ if test "not" != "equal";then echo Not Equal;fi Not Equal …]$ if [ "not" = "equal“ ];then echo Equal;fi …]$

  13. 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

  14. Testing Integers …]$ if test 123 -ne 234;then echo Not Equal;fi Not Equal …]$ if [ 123 -eq 234 ];then echo Equal;fi …]$

  15. Test Criteria (cont.) • Filename expression – file exists and has specific attributes • drewsx • -directory • -readable • -exists • -writable • -size – file has data (length >0) • -x executable

  16. Testing Files …]$ if [ ! -e “TestFile” ];then echo Not found;fi Not found …]$ …]$ if test -e “TestFile”;then echo Found it;fi …]$

  17. Bourne - if, then • if • Establishes a control structure • Followed by a test command • then • Commands executed if test is true

  18. Bourne – else, fi • else • Commands executed if test is false • fi • Terminates this if statement

  19. if …then … else … fi Test 1 Start False True

  20. Bourne - elif • elif • “else if” structure • Linear in nature • Similar to the case or switch in selection but completely different in execution.

  21. if …then … elif … else … fi Test 1 Test 2 Start False if elif else True True then

  22. if … then … [ else or elif ] … fi • 2 structures • if […] thencmdsif […] thencmdselsecmdselsecmdsfi • if […] thencmdselif[…] thencmdselsecmdsfi

  23. 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

  24. Control Structures • Iterations • for … in • for

  25. for… and for …in Assign element Do commands yes More elements no Done

  26. 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

  27. 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. 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

  29. 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

  30. Control Structures • Iterations • while • until

  31. While I’m here – Until it’s gone • while […](initial state true - go) • until […](initial state true – stop) • docmd cmd cmddone

  32. While you were out… cold Pre-Test Loop false While Test Done true do commands

  33. Until … The cows come home Pre-Test Loop true until Test Done false do commands

  34. Continue to Break • continue and break • Used in for while and until structures • break – terminates the structure • continue – terminates this iteration

  35. 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

  36. 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

  37. Bourne – case • The case command tests for multiple values in a variable • Allows the use of “wild cards” • First match wins

  38. Get on this case • case$varin test-var1) cmd1;cmd2 cmd3;;test-var2) cmds;;) cmds ;;esac

  39. 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)

  40. Topics • Control Structures • The Here Document • Expanding NULL or unset variables • The Builtins • Functions

  41. 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

  42. Here in the script #!/bin/bashgrep –i “$1” <<+Alex June 22Babs February 3Leroy January 20+ echo “All Done now”

  43. 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

  44. 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. Topics • Control Structures • The Here Document • Expanding NULL or unset variables • The Builtins • Functions

  46. 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. 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. Topics • Control Structures • The Here Document • Expanding NULL or unset variables • The Builtins • Functions

  49. Theexecutecommand • The exec command • Executes scripts or programs • Runs under the same PID • Provides access to the original environment variables • Terminates current process.

  50. Theexecutecommand • The exec command • Can be used to redirect stdin, stdout and stderr from inside a script. • exec < infile • exec > outfile 2> errfile

More Related