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

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

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

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

1© 2000 John Urrutia. All rights reserved.

Session 5

The Bourne Shell

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

2© 2000 John Urrutia. All rights reserved.

OverviewShell fundamentals

Streams revisited

Processes and Subshells

Shell script basics

Examples

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

3© 2000 John Urrutia. All rights reserved.

Shell fundamentalsCommand separation

Command grouping

Job control (limited in Bourne)

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

4© 2000 John Urrutia. All rights reserved.

Command SeparationNewline (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

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

5© 2000 John Urrutia. All rights reserved.

Command Separation (cont.)Ampersand (&)

execute task in the background

Pipe ( | ) pipe

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

6© 2000 John Urrutia. All rights reserved.

Command GroupingParenthesis 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

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

7© 2000 John Urrutia. All rights reserved.

Job ControlAmpersand &

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.

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

8© 2000 John Urrutia. All rights reserved.

Job Control (continued)First two jobs in background, c in

foregrounda & b & c

Entire sequence put into backgrounda | b | c &

All three jobs executed in backgrounda & b & c &

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

9© 2000 John Urrutia. All rights reserved.

Streams RevisitedThree streams

standard in < or 0<

standard out > or 1>

standard error 2>

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

10© 2000 John Urrutia. All rights reserved.

Streams standard I/Ocat 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

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

11© 2000 John Urrutia. All rights reserved.

Streams Continuedcat 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>>newfilestandard out is redirected to newfile

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

12© 2000 John Urrutia. All rights reserved.

Processes and SubshellsA 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

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

13© 2000 John Urrutia. All rights reserved.

Process FlowUser logs in: shell process is created

User issues command, enters returnShell creates a subshell

child process is forked or spawnedunless the command is built into the bourne

shell process

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

14© 2000 John Urrutia. All rights reserved.

Process flow (cont.)Subshell is a clone of the parent shell

Subshell tries to exec the commandIf program, the program runsIf shell script, exec fails and subshell

interprets commands.

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

15© 2000 John Urrutia. All rights reserved.

Process FlowParent 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.

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

16© 2000 John Urrutia. All rights reserved.

Process FlowShell 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

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

17© 2000 John Urrutia. All rights reserved.

Shell Script BasicsCreating a Shell Script

Keyword Shell Variables

User Created Variables

Readonly Shell Variables

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

18© 2000 John Urrutia. All rights reserved.

Creating a Shell ScriptUse 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)

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

19© 2000 John Urrutia. All rights reserved.

Keyword Shell VariablesHOME (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)

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

20© 2000 John Urrutia. All rights reserved.

Keyword Variables (continued)CDPATH

like PATH, except used by cd command

TZtimezone

IFSInternal field separator. Blanks and tabs

are default

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

21© 2000 John Urrutia. All rights reserved.

User Created VariablesCreate a variable by giving a name of your

choice and an optional valuename=charlie

NO blanks around the equal sign!!

Remove variableunset name

Keep variable but remove valuename=

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

22© 2000 John Urrutia. All rights reserved.

Readonly Shell VariablesTwo types: user created variable that

has been declared to be readonlyreadonly name

keeps later statements from changing the value

Special Shell variables Positional Variables

Miscellanous variables

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

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

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

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

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

25© 2000 John Urrutia. All rights reserved.

Shift commandPromotes 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)

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

26© 2000 John Urrutia. All rights reserved.

Boolean ExpressionsAlgebra created by George Boole

Always evaluates to a binary stateGenerally:

1 is TRUE0 is FALSE

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

27© 2000 John Urrutia. All rights reserved.

Boolean Operators

And-a

Or-o

Not !

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

28© 2000 John Urrutia. All rights reserved.

and Boolean Truth Tables

Expr 1 Expr 2 -a

True True True

FalseTrue False

False True False

False False False

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

29© 2000 John Urrutia. All rights reserved.

or Boolean Truth Tables

True True True

FalseTrue True

False True True

False False False

Expr 1 Expr 2 -o

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

30© 2000 John Urrutia. All rights reserved.

not Boolean Truth Tables

Expr !

FalseTrue

False True

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

31© 2000 John Urrutia. All rights reserved.

test ing, test ing, test ingtest expression or [ … ]

Evaluates the expression and returns a Boolean true or false.

expression can be simple or compound

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

32© 2000 John Urrutia. All rights reserved.

Test CriteriaString expresions

Is null orLength is zero (-z) orLength is > 0 (-n)

string1 = or != string2

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

33© 2000 John Urrutia. All rights reserved.

Test Criteria (cont.)Filename expression

File exists and has specific attributes -block -character -directory -size – file has data (length >0)

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

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

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

35© 2000 John Urrutia. All rights reserved.

Bourne - if, thenif

Establishes a control structureFollowed by a test command

thenCommands executed if test is true

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

36© 2000 John Urrutia. All rights reserved.

Bourne – else, fielse

Commands executed if test is false

fiTerminates this if statement

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

37© 2000 John Urrutia. All rights reserved.

Bourne - elifelif

“else if” structureLinear in nature

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

38© 2000 John Urrutia. All rights reserved.

if $a –gt 2then

if $a –gt 3then

echo value is 4 else

echo value is 3fi

elseif $a –gt 1then

echo value is 2else

echo value is 1fi

if $a –gt 3then

echo value is 4elif $a –gt 2

thenecho value is 3

elif $a –gt 1 then

echo value is 2else

echo value is 1fi

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

39© 2000 John Urrutia. All rights reserved.

Bourne – caseThe case command tests for

multiple values in a variable

Allows the use of “wild cards”

First match wins

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

40© 2000 John Urrutia. All rights reserved.

case “$variable” inA|a )

echo Entered A or a;;

[0-9] )echo Entered number;;

?z* )echo Entered z in 2nd position;;

esac

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

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#-----------------------------------------------------------------#

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

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

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

43© 2000 John Urrutia. All rights reserved.

Additions to myscriptecho 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