Experiment No. - 9 - Advanced Shell Programming 1. Objective(s) : The Activity Aims
Experiment No. - 9 - Advanced Shell Programming 1. Objective(s) : The Activity Aims
___9__
ADVANCED SHELL PROGRAMMING
1. Objective(s):
The activity aims
1.1 To evaluate programming skills using advanced shell programming
1.2 To create shell scripts with decision making techniques
Just like in any programming language, shell programming is also equipped with decision making
features. In such a case, you provide two alternative sets of commands, and the shell program executes
the set of commands depending on a specified criteria.
4. Resources:
Personal computer with Unix or unix-based operating system
5. Procedure:
5.1. Start a terminal session with your unix or unix-based operating system.
5.2. Create and test shell scripts decision-making and looping techniques
5.2.1 Making Decisions with if. Making decisions has always been part of our daily lives. Solving
the exercises in this manual or not is a decision that the students have to make if you want
to facilitate the learning of the UNIX operating system.
Just like in any programming language, shell programming is also equipped with decision
making features. In such a case, you provide two alternative sets of commands, and the
shell program executes the set of commands depending on a specified criteria.
The if command allows the shell program to make a decision, that is by evaluating certain
conditions within the program. The shell then executes the set of command where the
condition evaluates to true and ignores the other set of command.
The decision making process of the UNIX operating system is based on the concept of
success and failure. The decision depends on the individual commands. But, what is
success and what is failure. Whenever a command completes its task without error, then
the command is successful. On the other hand, when the command aborts and displays an
error, then the command is a failure. For example, listing the permission of a file, when a
file is not existent or printing to an unknown printer.
5.2.3
Performing tests with test. The test command enables you to evaluate for conditions other than
success or failure. The command succeeds or fails based on whether a given condition is true or
false.
The test command can determine whether a specified file meets a desired criterion. It has
the following form:
test [option] filename
Commonly Used Options for the test Command
Option Action
-s Checks whether a file exists and has a size greater than zero
Modify the previous exercise and use the test command to produce a better output:
The program behaves similar to the previous program, if the file is available. It behaves
differently, otherwise. There was no error message displayed, if the file is not found; rather,
just the friendly message is display.
Alternative Format of test
The test command has an alternative format that doesn’t even use the word test. That is,
by enclosing the condition in brackets, as follows:
[ condition ]
As in,
If [ -f $filename ]
Please observe the spaces between the condition and the brackets.
5.2.4 Performing Variable Tests. Probably the most important use of for test is checking the value
of a variable. This test falls in two classes: string tests and numeric tests. 5.2.4.1. The
format of the string test is as follows:
test value operation value
where: value represents a string, or a variable that contains a string
operation can either be = (equality) or != (difference)
Operation Meaning
5.2.5 Using Programming Loops. Perhaps the most important ability of any programming language
is performing repetitive tasks. People get bored and tired doing the same thing over and
over again, but not the computers. They can perform repeated works with extreme
efficiency and accuracy.
A loop is a generic computer term for an operation done repeatedly. There are two
commands that enable you to repeat tasks: for and while.
5.2.5.1. Looping with for. The for command repeatedly performs a set of commands on a
list of items, which could be anything such as user names, file names, a list of
students. The for command has this format:
for variable
i n list
do
commands
done
Alternatively, the for and in can be written on the same line as follows:
The commands between the do and done will be performed as many times as
there are items in the list. The first time that the commands are executed, the
variable takes the value of the first item in the list; the second time, gets the
value of the second item in the list, and so on until all the items in the list are
assigned.
The exercise below lists the shell programs of a directory:
5.2.5.2. Looping with while. The shell’s other looping command is while, and uses the
following format:
while
One or more commands
do
One or more commands
done
The while loop resembles the if command. In fact, the while command works the
same way as the if command. It executes all the commands between the while
and the do statements, checking whether the last command succeeds or fails. If
the last command succeeds, the commands between the do and the done
statements will be executed; otherwise, if it fails, these commands are skipped and
shell executes the commands following the done statement.
The exercise below demonstrates the use of the while command. It asks for the
subject you enrolled, one per line. The program asks the same question
repeatedly until you enter the word ‘exit’.
Course: Experiment No.:
Group No.: Section:
Group Members: Date Performed:
Date Submitted:
Instructor: