430 likes | 554 Views
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’
E N D
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’ • 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
Command Separation (cont.) • Ampersand (&) • execute task in the background • Pipe ( | ) • pipe
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
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.
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 &
Streams Revisited • Three streams • standard in < or 0< • standard out > or 1> • standard error 2>
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
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
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
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
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.
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.
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
Shell Script Basics • Creating a Shell Script • Keyword Shell Variables • User Created Variables • Readonly Shell Variables
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)
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)
Keyword Variables (continued) • CDPATH • like PATH, except used by cd command • TZ • timezone • IFS • Internal field separator. Blanks and tabs are default
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=
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
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
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
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)
Boolean Expressions • Algebra created by George Boole • Always evaluates to a binary state • Generally: • 1 is TRUE • 0 is FALSE
Boolean 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 • testexpression or [ … ] • Evaluates the expression and returns a Boolean true or false. • expression can be simple or compound
Test Criteria • String expresions • Is null orLength is zero (-z) orLength is > 0 (-n) • string1 = or != string2
Test Criteria (cont.) • Filename expression • File exists and has specific attributes • -block-character-directory • -size – file has data (length >0)
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
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
Bourne - elif • elif • “else if” structure • Linear in nature
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
Bourne – case • The case command tests for multiple values in a variable • Allows the use of “wild cards” • First match wins
case “$variable” in A|a ) echo Entered A or a ;; [0-9] ) echo Entered number ;; ?z* ) echo Entered z in 2nd position ;; esac
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 #-----------------------------------------------------------------#
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.$$
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