Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS465 - UNIX The Bourne Shell.

Similar presentations


Presentation on theme: "CS465 - UNIX The Bourne Shell."— Presentation transcript:

1 CS465 - UNIX The Bourne Shell

2 Bourne Shell The Bourne shell was the first widely used UNIX shell. It is: the most common the easiest to learn used most often by beginning users This week we will cover the Bourne shell and its scripting features. Bourne scripts are portable to all versions of Unix.

3 Shells A shell can be used in one of two ways:
A command interpreter, used interactively A programming language, to write shell scripts (your own custom programs)

4 Shell Scripts A shell script is just a file containing shell commands:
The first line of a shell script should be a comment of the following form: #!/bin/sh (Bourne) #!/bin/ksh (Korn) A line beginning with a hash character (#) is usually a comment. But #! is a special case: Tells the shell which interpreter to run the lines of the script under If you leave it off, commands will be interpreted by your interactive shell (in our case, ksh)

5 Shell Scripts Each line in a script file contains a command you could have typed at the command prompt A shell script must be readable and executable. $ chmod +x scriptname $ chmod 700 scriptname The shell script file has to be “in your path” to be executed. If the current directory (symbolized by . ) is not in your PATH, you must type ./scriptname to run it.

6 Simple Shell Script Example
Here is a “hello world” shell script: $ ls –lF h* -rwxr-xr-x 1 smith 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh # comment lines start with # character echo "Hello world" $ hello Hello world $

7 Variable Names The shell provides for variable creation and usage.
There are basically two general categories of variables in UNIX: Shell Variables (Assigned by the Shell) User-Created Variables (Assigned by the user)

8 Displaying All Shell Variables
Use the set command alone to display all variables: $ set HOME=/home/small000 LOGNAME=small000 MAIL=/usr/mail/small000 PATH=/bin:/usr/bin:/etc: PPID=24244 PS1='$ ' PS2='> ' : $

9 User-Created Variables
A user-created variable name can be any sequence of letters, digits, and the underscore character, but the first character must be a letter. You do not have to “declare” variables. To create a new variable, simply assign a value to a variable name variableName=value

10 Assigning User-Created Variables
Assigning values to a variables: number= job=engineer name="John Doe" There cannot be any spaces before or after the equal sign (=). (And there is no semi-colon!) Use double quotes to store strings containing with white space. NOTE: Internally, ALL values are stored as strings (even the numbers)

11 Using User-Created Variables
To use a variable’s value, precede the variable name with a $ $ cat test1 #!/bin/sh number=25 name="John Doe" echo "$number $name" echo '$number $name' $ test1 25 John Doe $number $name $

12 Using Backquotes with Variables
Remember that backquotes (graves) force command execution. Can use backquotes for variable assignment: $ cat test2 #!/bin/sh user=`whoami` numusers=`who | wc -l` echo "Hi $user! $numusers users logged on." $ test2 Hi small000! users logged on. $

13 De-assigning User-Created Variables
To eliminate a previously stored variable value: - assign a “NULL” value to the variable OR - use unset command. Examples: name= unset number

14 Displaying Dollar Signs
Use a backslash before the $ OR Use single quotes around the dollar sign $ cat test4 #!/bin/sh cost=18.50 echo "The total is: \$$cost " echo 'Again, the total is $' $cost $ test4 Enter amount: 18.50 The total is $18.50 Again, the total is $18.50 $

15 Displaying Quotes Enclose single quotes in double quotes
Enclose double quotes in single quotes Or backslash the desired character $ cat test5 #!/bin/sh echo "Doubles protect 'singles' " echo 'Singles protect "doubles" ' echo Single \' or double \" $ test5 Doubles protect 'singles' Singles protect "doubles" Single ' or double " $

16 Long Lines Within a script, a command can be "wrapped" to the next line by making the last character a backslash: Example: $ cat homedir echo "My home directory is" \ $HOME ls $HOME $ homedir My home directory is /home/smith file1 file2 file3 $

17 Reading User Input read is the shell's input command; it reads a line of input and stores the result in one or more variables read’s arguments are the list of variables to store the input in: If you only list one variable, the entire line of input will be stored in it If you list more than one variable, the line will be split into words, using white space as the delimiter, and each variable will get one word If there are more words than variables, the last variable will store all the remaining words

18 Reading User Input Use the echo command to prompt the user
Then use the read command to read a line of text into a variable. Example: echo –n "Enter your full name: " read fullname -n option prompts user for data on same line as message...

19 User Input within Scripts
$ cat test2 #!/bin/sh echo "Enter name: " read name echo "How many classes have you taken? " read number echo "$name has taken $number classes " $ test2 Enter name: Joe Smith How many classes have you taken? too many Joe Smith has taken too many classes! $

20 Missing User Inputs $ cat test3 #!/bin/sh
echo "Enter name and number of classes: " read name number echo "$name has taken $number classes!" $ test3 Enter name and number of classes: Joe Joe has taken classes! $ $ test3 Enter name and number of classes: 25 25 has taken classes! $

21 Dividing Multiple User Inputs
$ cat test3 #!/bin/sh echo "Enter name and number of classes: " read name number echo "$name has taken $number classes!" $ test3 Enter name and number of classes: Joe Smith 25 Joe has taken Smith 25 classes! $ Spaces indicate the end of input AND the last variable will read the input all the way to the end of the line.

22 Quoting Multiple User Inputs
$ cat test3 #!/bin/sh echo "Enter name and number of classes: " read name number echo "$name has taken $number classes!" $ test3 Enter name and number of classes: "Joe Smith" 25 "Joe has taken Smith" 25 classes! $ Quoting the input does NOT override spaces as “end of variable input” indicators.

23 Variable Processing Special access ${var}
Same as $var, but useful when variable name is followed by text $ dept=CS $ echo UNIX is ${dept}465 UNIX is CS465 $ $ echo ${USER}\'s home dir is $HOME brown345's home dir is /home/brown345 $

24 Positional Parameter Shell Variables
Shell variables are used to store commands and related arguments You can use the set command to assign values to shell variables When you create shell scripts, the arguments after the script name are automatically stored into shell variables

25 Using the set command $1 to $9 are the first nine positional parameters To use the set command: $ set Hello there class! Results: $1 = Hello $2 = there $3 = class!

26 Positional Parameters
The output of a command can be used to set the positional shell variables: $ date Fri May 16 11:04:03 MDT 2003 $ set `date` $ echo The time is $4 The time is 11:04:03 $ $* matches all variables $1 - $9 echo $* Fri May 16 11:04:03 MDT 2003 $ $ echo Today is $3 $2 $6 Today is 16 May 2003 $

27 Positional Parameters within Scripts
$0 is name of command used to call script $1 to $9 are first nine positional parameters $* represents all positional parameters as one long string "$*" will put single pair of double quotes around the one long string will put double quotes around each individual parameter $# contains number of parameters on the command line

28 $0 parameter $0 holds the name of the command the user typed to invoke the shell script: $ cat print1 #!/bin/sh echo "This script is called $0" $ print1 This script is called print1 $ ./print1 This script is called ./print1 $ ~/print1 This script is called /usr/smith/print1 $

29 Using Positional Parameters
Example: A command to swap two files: $ cat swap #!/bin/sh mv $1 tempfile mv $2 $1 mv tempfile $2 rm tempfile $ cat notes1 contents of notes1 $ cat notes2 contents of notes2 $ swap notes1 notes2 $ cat notes1 contents of notes2 $ cat notes2 contents of notes1 $

30 Using $* and $# $* lists all the command line args:
$ cat args1 #!/bin/sh echo The args are $* $ args1 a1 a2 a3 a4 a5 a6 a7 a8 The args are a1 a2 a3 a4 a5 a6 a7 a8 $ $# contains the number of args: $ cat args2 echo The number of args is $# $ args2 a1 a2 a3 a4 a5 a6 a7 a8 The number of args is 8

31 Shifting Positional Parameters
Under the Bourne shell, you can only refer directly to 10 positional parameters: $0 to $9 Note: Korn shell allows ${10} etc Can “pull” parameters beyond $9 into view with shift command: shift shifts parameters $1 - $9 left, allowing parameters beyond the first nine to be accessed Arguments "falling off" the left side are no longer available, so should be saved in other variables before the shift if they will be required later in the script

32 Shifting Positional Parameters
After a shift $2 becomes $1 $3 becomes $2 .... ${10} becomes $9 Note: $0 does NOT change shift nn, where nn is a number, has the same effect as that number of shift commands

33 shift Example The previous $1 becomes inaccessible each time #!/bin/sh
$ cat shiftargs #!/bin/sh echo $* echo "Args: \$0 = $0, \$1 = $1, \$2 = $2" shift $ shiftargs arg1 arg2 arg3 arg1 arg2 arg3 Args: $0 = shiftarg, $1 = arg1, $2 = arg2 Args: $0 = shiftarg, $1 = arg2, $2 = arg3 Args: $0 = shiftarg, $1 = arg3, $2 = The previous $1 becomes inaccessible each time

34 Locality of Variables By default, all shell variables you create are only available locally within your current shell (not from within a script or command subshells) $ cat showdept echo $dept $ dept=CS $ echo $dept CS $ showdept $ All scripts are run in subshells, and dept was created in the parent shell, it is local to the parent shell and not available to the subshell.

35 Exporting Variables How can we make a variable available to script and command subshells? Use the export command to export the variable to all subshells: $ export varname 

36 Exported Variable Example
$ class=465 $ export class $ cat showclass echo CS $class $ showclass CS 465 $ class was created in the parent shell and exported. So it is availabe to use by any scripts running in subshells.

37 Exported Variable Values
When a shell variable is exported, a COPY of the variable's value is made available to subshells. If the COPY's value is changed within the subshell, that change is NOT passed back to the parent shell The change is only accessible while you are still in the subshell.

38 Exported Variable Example
$ class=Unix $ export class $ cat changeclass echo In $0 start: $class class=Pascal echo In $0 end: $class $ changeclass In changeclass start: Unix In changeclass end: Pascal $ echo $class Unix $ class has not changed in the parent shell.

39 Command Exit Values When a command completes it sets an integer called the return or exit status Conventionally: zero means no error non-zero means an error of some kind The shell control flow constructs will all take a zero status to mean true and a non-zero status to mean false

40 Script Exit Status $? is the exit status of the last command that was run Script can end with exit n where n is the exit status or condition code of the script Example: $ cat example echo Hello exit 0 $ example Hello $ echo $? $

41 Invoking a Shell Script
$ scriptname Run a separate copy of the shell (a subshell) to run the script (like we have been doing) $ . scriptname (dot space scriptname) Run the script within the current invocation of the shell $ shellname scriptname Run a separate copy of the named shell to run the script

42 Special variable - $$ $$ is the process ID (PID) of the current process (the shell script PID, or the shell PID if interactive). $ ps PID TTY TIME CMD 892 pts/0 0:01 csh $ echo $$ 892 $ cat pidscript #!/bin/sh echo $$ $ $ pidscript 1154 1156 $

43 Running Under a Different Shell
$ cat newsh ps echo script PID is $$ $ csh newsh PID TTY TIME CMD 15083 pts/16 0:00 csh 15042 pts/16 0:00 ksh script PID is 15083 $ echo $$ 15042 $ Note: Overrides #! Setting in script

44 Running without a Subshell (under the current shell)
$ echo $$ 78392 $ cat testprog echo $0 PID is $$ $ . testprog testprog PID is 78392 $

45 wait command The wait command will cause a parent shell to wait until all children have completed. Can wait for a specific child if optional specific child PID is specified; otherwise waits for ALL children to complete. Format: wait <child-PID>

46 Using wait $ cat script1 echo $0 running. . . sleep 5 echo $0 done
$ cat waitscript echo $0 waiting. . . script1 & wait echo child is done echo exiting $0 $ waitscript waitscript waiting (parent) script1 running (child) script1 done (child) child is done (parent) exiting waitscript (parent) $


Download ppt "CS465 - UNIX The Bourne Shell."

Similar presentations


Ads by Google