Post on 20-Jan-2016
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
3© 2000 John Urrutia. All rights reserved.
Shell fundamentalsCommand separation
Command grouping
Job control (limited in Bourne)
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
5© 2000 John Urrutia. All rights reserved.
Command Separation (cont.)Ampersand (&)
execute task in the background
Pipe ( | ) pipe
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
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.
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 &
9© 2000 John Urrutia. All rights reserved.
Streams RevisitedThree streams
standard in < or 0<
standard out > or 1>
standard error 2>
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
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
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
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
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.
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.
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
17© 2000 John Urrutia. All rights reserved.
Shell Script BasicsCreating a Shell Script
Keyword Shell Variables
User Created Variables
Readonly Shell Variables
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)
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)
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
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=
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
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© 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© 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)
26© 2000 John Urrutia. All rights reserved.
Boolean ExpressionsAlgebra created by George Boole
Always evaluates to a binary stateGenerally:
1 is TRUE0 is FALSE
27© 2000 John Urrutia. All rights reserved.
Boolean Operators
And-a
Or-o
Not !
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
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
30© 2000 John Urrutia. All rights reserved.
not Boolean Truth Tables
Expr !
FalseTrue
False True
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
32© 2000 John Urrutia. All rights reserved.
Test CriteriaString expresions
Is null orLength is zero (-z) orLength is > 0 (-n)
string1 = or != string2
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)
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© 2000 John Urrutia. All rights reserved.
Bourne - if, thenif
Establishes a control structureFollowed by a test command
thenCommands executed if test is true
36© 2000 John Urrutia. All rights reserved.
Bourne – else, fielse
Commands executed if test is false
fiTerminates this if statement
37© 2000 John Urrutia. All rights reserved.
Bourne - elifelif
“else if” structureLinear in nature
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
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
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
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© 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© 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