Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.

Similar presentations


Presentation on theme: "1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell."— Presentation transcript:

1 1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell

2 2 © 2000 John Urrutia. All rights reserved. Overview Shell fundamentals Streams revisited Processes and Subshells Shell script basics Examples

3 3 © 2000 John Urrutia. All rights reserved. Shell fundamentals Command separation Command grouping Job control (limited in Bourne)

4 4 © 2000 John Urrutia. All rights reserved. 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 5 © 2000 John Urrutia. All rights reserved. Command Separation ( cont. ) Ampersand (&)  execute task in the background Pipe ( | )  pipe

6 6 © 2000 John Urrutia. All rights reserved. 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 7 © 2000 John Urrutia. All rights reserved. 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 8 © 2000 John Urrutia. All rights reserved. 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 9 © 2000 John Urrutia. All rights reserved. Streams Revisited Three streams  standard in < or 0<  standard out > or 1>  standard error 2>

10 10 © 2000 John Urrutia. All rights reserved. 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 11 © 2000 John Urrutia. All rights reserved. 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 12 © 2000 John Urrutia. All rights reserved. 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 13 © 2000 John Urrutia. All rights reserved. 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 14 © 2000 John Urrutia. All rights reserved. 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 15 © 2000 John Urrutia. All rights reserved. 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 16 © 2000 John Urrutia. All rights reserved. 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 17 © 2000 John Urrutia. All rights reserved. Shell Script Basics Creating a Shell Script Keyword Shell Variables User Created Variables Readonly Shell Variables

18 18 © 2000 John Urrutia. All rights reserved. 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 19 © 2000 John Urrutia. All rights reserved. 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 20 © 2000 John Urrutia. All rights reserved. Keyword Variables (continued) CDPATH  like PATH, except used by cd command TZ  timezone IFS  Internal field separator. Blanks and tabs are default

21 21 © 2000 John Urrutia. All rights reserved. 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 22 © 2000 John Urrutia. All rights reserved. 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 23 © 2000 John Urrutia. All rights reserved. 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 24 © 2000 John Urrutia. All rights reserved. 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 25 © 2000 John Urrutia. All rights reserved. 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 26 © 2000 John Urrutia. All rights reserved. Boolean Expressions Algebra created by George Boole Always evaluates to a binary state  Generally:  1 is TRUE  0 is FALSE

27 27 © 2000 John Urrutia. All rights reserved. Boolean Operators And  -a Or  -o Not  !

28 28 © 2000 John Urrutia. All rights reserved. and Boolean Truth Tables Expr 1Expr 2 -a True FalseTrue False TrueFalse

29 29 © 2000 John Urrutia. All rights reserved. or Boolean Truth Tables True FalseTrue FalseTrue False Expr 1Expr 2 -o

30 30 © 2000 John Urrutia. All rights reserved. not Boolean Truth Tables Expr ! FalseTrue FalseTrue

31 31 © 2000 John Urrutia. All rights reserved. test ing, test ing, test ing test expression or [ … ]  Evaluates the expression and returns a Boolean true or false.  expression can be simple or compound

32 32 © 2000 John Urrutia. All rights reserved. Test Criteria String expresions  Is null or Length is zero (-z) or Length is > 0 (-n)  string1 = or != string2

33 33 © 2000 John Urrutia. All rights reserved. Test Criteria (cont.) Filename expression  File exists and has specific attributes  -b lock -c haracter -d irectory  -size – file has data (length >0)

34 34 © 2000 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

35 35 © 2000 John Urrutia. All rights reserved. Bourne - if, then if  Establishes a control structure  Followed by a test command then  Commands executed if test is true

36 36 © 2000 John Urrutia. All rights reserved. Bourne – else, fi else  Commands executed if test is false fi  Terminates this if statement

37 37 © 2000 John Urrutia. All rights reserved. Bourne - elif elif  “else if” structure  Linear in nature

38 38 © 2000 John Urrutia. All rights reserved. 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 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

39 39 © 2000 John Urrutia. All rights reserved. Bourne – case The case command tests for multiple values in a variable Allows the use of “wild cards” First match wins

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

41 41 © 2000 John Urrutia. All rights reserved. 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 42 © 2000 John Urrutia. All rights reserved. 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 43 © 2000 John Urrutia. All rights reserved. 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


Download ppt "1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell."

Similar presentations


Ads by Google