1 / 43

Session 5

Session 5. The Bourne Shell. Overview. Shell fundamentals Streams revisited Processes and Subshells Shell script basics Examples. Shell fundamentals. Command separation Command grouping Job control (limited in Bourne). Command Separation. Newline (nl) X’0D0A’

kamali
Download Presentation

Session 5

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. Session 5 The Bourne Shell

  2. Overview • Shell fundamentals • Streams revisited • Processes and Subshells • Shell script basics • Examples

  3. Shell fundamentals • Command separation • Command grouping • Job control (limited in Bourne)

  4. Command Separation • Newline (nl) X’0D0A’ • ends command and initiates execution • Semicolon (;) • just separates commands • Backslash (\) X’5C0D0A’ • at end of line and before you type return Allows command to be continued

  5. Command Separation (cont.) • Ampersand (&) • execute task in the background • Pipe ( | ) • pipe

  6. Command Grouping • Parenthesis used to group commands • causes Shell to create a subshell • additional processes are created as required when the subshell runs the commands within the parenthesis • (ls ; date; w) ; more • (ls ; date; w) | more

  7. Job Control • Ampersand & • tells the Operating system to run the job in the background • User will still be able to interact with the shell • Pure Bourne shell has limited ability. Can not deal with a specific job it has put into background after initial creation. C shell much better.

  8. Job Control (continued) • First two jobs in background, c in foreground • a & b & c • Entire sequence put into background • a | b | c & • All three jobs executed in background • a & b & c &

  9. Streams Revisited • Three streams • standard in < or 0< • standard out > or 1> • standard error 2>

  10. Streams standard I/O • cat x y • if x exists and y does not, contents of x and error message due to y are sent to terminal • both standard out and standard error default to the terminal

  11. Streams Continued • cat x y 2>error.log • standard error is sent to a file to separate it from the expected results of the command • cat x y 2>>newfile 1>>newfile • standard out is redirected to newfile

  12. Processes and Subshells • A process is the execution of a command • login to UNIX • execution of a UNIX utility • execution of a shell script creates a new process • script commands each start a new process • Process structure is hierarchical

  13. Process Flow • User logs in: shell process is created • User issues command, enters return • Shell creates a subshell • child process is forked or spawned • unless the command is built into the bourne shell process

  14. Process flow (cont.) • Subshell is a clone of the parent shell • Subshell tries to exec the command • If program, the program runs • If shell script, exec fails and subshell interprets commands.

  15. Process Flow • Parent Shell sleeps until child shell finishes • (unless job was executed in background) • Variables that are used in a parent can be sent to a child, but the reverse is not true.

  16. Process Flow • Shell Scripts need to have execute permission if you just type the file name as you would a command • Alternative (new subshell): sh file • Alternative (current shell): • file

  17. Shell Script Basics • Creating a Shell Script • Keyword Shell Variables • User Created Variables • Readonly Shell Variables

  18. Creating a Shell Script • Use a text editor like vi • First line should start with #! Followed by the absolute pathname of the shell that is to interpret the script. (default is C shell) • #!/bin/sh • Lines which start with a # are comments • (except the special line mentioned above)

  19. Keyword Shell Variables • HOME (contains login directory) • PATH (Used by shell to locate commands you type in) • /usr/bin:/usr/sbin:/class/n01/bin: • MAIL (contains name of central post office file for your mail) • PS1, PS2 (primary and secondary prompts)

  20. Keyword Variables (continued) • CDPATH • like PATH, except used by cd command • TZ • timezone • IFS • Internal field separator. Blanks and tabs are default

  21. User Created Variables • Create a variable by giving a name of your choice and an optional value • name=charlie • NO blanks around the equal sign!! • Remove variable • unset name • Keep variable but remove value • name=

  22. Readonly Shell Variables • Two types: user created variable that has been declared to be readonly • readonly name • keeps later statements from changing the value • Special Shell variables • Positional Variables • Miscellanous variables

  23. Positional Variables • $1 through $9 • Keep the first nine arguments entered after the name of the shell script • myscript aardvark dog cat • $1 will contain the word aardvark • $2 will contain the word dog • $3 will contain the word cat

  24. Miscellaneous Variables • $* contains all arguments (not just the first one) • $@ similar to $*, except that it internally quotes each argument. • $# total number of arguments • $$ process id number (pid) of current process

  25. Shift command • Promotes values of each positional variable to the left. • Contents of $1 go to ‘bit bucket’ • Contents of $2 go to $1 • Contents of $3 go to $2 • etc (etcetera, not etci)

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

  27. Boolean Operators • And • -a • Or • -o • Not • !

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

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

  30. not Boolean Truth Tables ! True False False True

  31. test ing,test ing,testing • testexpression or [ … ] • Evaluates the expression and returns a Boolean true or false. • expression can be simple or compound

  32. Test Criteria • String expresions • Is null orLength is zero (-z) orLength is > 0 (-n) • string1 = or != string2

  33. Test Criteria (cont.) • Filename expression • File exists and has specific attributes • -block-character-directory • -size – file has data (length >0)

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

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

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

  37. Bourne - elif • elif • “else if” structure • Linear in nature

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

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

  40. case “$variable” in A|a ) echo Entered A or a ;; [0-9] ) echo Entered number ;; ?z* ) echo Entered z in 2nd position ;; esac

  41. Examples: Myscript #!/bin/sh #-----------------------------------------------------------------# # Script Name: myscript # Written by: Charlie Verboom # Date: 9/19/97 # Purpose: demonstrate the use of # variables and displays upon terminal # Arguments: 3 arguments used to set # variables to be displayed #-----------------------------------------------------------------#

  42. Myscript (continued) echo the first argument is $1 echo the second argument is $2 echo the third argument is $3 echo $# arguments were typed in echo the current process number is $$ echo script executed successfully> log.$$

  43. Additions to myscript echo Please type in your name read name echo You typed in $name echo Please type in your age age=`head -1` echo Your age is $age

More Related