Professional Documents
Culture Documents
Shell Scripts - Control Structure: CS1101: Lecture 10
Shell Scripts - Control Structure: CS1101: Lecture 10
CS1101: Lecture 10
Shell Scripts Control
Structure Lecture Outline
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
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 2 Department of Computer Science, University College Cork 3
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 4 Department of Computer Science, University College Cork 5
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 6 Department of Computer Science, University College Cork 7
Using elif and else The case Statement
We can make the selection structures much The shell provides another selection structure
more elaborate by combining the if statement that may run faster than the if statement on
with the elif (else if) and else statements. some UNIX systems.
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
#!/bin/sh
set date
case $1 in
esac
Department of Computer Science, University College Cork 10 Department of Computer Science, University College Cork 11
for Loops while Loops
Sometimes we want to run a command (or The general form of the while loop is:
group of commands) over and over.
while condition
This is called iteration, repetition, or looping.
do
command(s)
The most commonly used shell repetition
done
structure is the for loop, which has the general
form:
As long as the condition is true, the commands
for variable in list between the do and the done are executed.
do
command(s) Example
done
#!/bin/sh
Here is a simple example: # Print a message ten times
count=10
#!/bin/sh while test $count -gt 0
for host in $* do
do echo $*
ping $host count=expr $count - 1
done done
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
Another kind of iteration structure is the until We can rewrite the previous script using an
loop. until loop instead of the while loop:
Department of Computer Science, University College Cork 14 Department of Computer Science, University College Cork 15