CS1101: Lecture 9: The Shell As A Programming Language
CS1101: Lecture 9: The Shell As A Programming Language
CS1101: Lecture 9
The Shell as a Programming
Language Counting Arguments
Control Structures
The if Statement
Example: if Statement
The test Command
Using elif and else
Course Homepage
http://www.cs.ucc.ie/osullb/cs1101 Taken from: Anderson Just Enough UNIX
Department of Computer Science, University College Cork Department of Computer Science, University College Cork 1
CS1101: Systems Organisation UNIX Shell Scripting CS1101: Systems Organisation UNIX Shell Scripting
The parameter $# contains the number of The positional parameters are useful for
arguments that the user typed. capturing command-line arguments but they
have a limitation: once the script begins
We can modify the script echo.args once running, the positional parameters cannot be
again to use this parameter: used for obtaining more input from the standard
input.
#!/bin/sh
# Illustrate the use of positional parameters For this you have to use the read statement.
echo You typed $# arguments: $*
Lets modify the previous program to make use
Suppose we were then to type the command of read:
line
#!/bin/sh
$ echo.args To be or not to be # Illustrate the use of positional paramete
# variables and the read command.
The computer would respond with echo What is your name?
read name
You typed 6 arguments: To be or not to be echo "Well, $name, you typed $# arguments:
echo "$*"
Department of Computer Science, University College Cork 2 Department of Computer Science, University College Cork 3
In this script, name is a user-defined variable.
The set Command
#!/bin/sh
# Demonstrate the set command
set date
echo "Time: $4 $5"
echo "Day: $1"
echo "Date: $3 $2 $6"
Department of Computer Science, University College Cork 4 Department of Computer Science, University College Cork 5
CS1101: Systems Organisation UNIX Shell Scripting CS1101: Systems Organisation UNIX Shell Scripting
Assuming that setdate has been made The backquotes in the set date command
executable with the chmod command, we run the UNIX date command, which produces
can run the script by typing setdate as a output something like this:
command name
Fri Aug 10 10:56:08 EST 2001
The output will look something like this:
This does not appear on the screen.
Time: 10:56:08 EST
Day: Fri Instead, the set command catches the output
Date: 10 Aug 2001 in the positional parameters $1 through $6:
$1 contains Fri
What happened?
$2 contains Aug
$3 contains 10
$4 contains 10:56:08
$5 contains EST
$6 contains 2001
Department of Computer Science, University College Cork 6 Department of Computer Science, University College Cork 7
Arithmetic using expr Arithmetic using expr
Department of Computer Science, University College Cork 8 Department of Computer Science, University College Cork 9
CS1101: Systems Organisation UNIX Shell Scripting CS1101: Systems Organisation UNIX Shell Scripting
Normally, the shell processes the commands There are two types of selection structures,
in a script sequentially, one after another in the which allow a choice between alternative
order they are written in the file. commands:
if/then/elif/else/fi
Often, however, you will want to change the
case
way that commands are processed.
There are three types of repetition or iteration
You may want to choose to run one command
structures for carrying out commands more
or another, depending on the circumstances;
than once:
or you may want to run a command more than
once. for
while
To alter the normal sequential execution of until
commands, the shell offers a variety of control
structures.
Department of Computer Science, University College Cork 10 Department of Computer Science, University College Cork 11
If the conditional expression is not true, the
The if Statement shell skips the commands between then and
fi.
if conditional expression
then
command(s)
fi
Department of Computer Science, University College Cork 12 Department of Computer Science, University College Cork 13
CS1101: Systems Organisation UNIX Shell Scripting CS1101: Systems Organisation UNIX Shell Scripting
Here is an example of a shell script that uses a Here we have used the test command in our
simple if statement: conditional expression.
Department of Computer Science, University College Cork 14 Department of Computer Science, University College Cork 15
Using elif and else
#!/bin/sh
set date
if test $1 = Fri
then
echo "Thank goodness its Friday!"
elif test $1 = Sat 11 test $1 = Sun
then
echo "You should not be here working."
echo "Log off and go home."
else
echo "It is not yet the weekend."
echo "Get to work!"
fi