OS_LAB_06
OS_LAB_06
Operating Systems
Experiment 6
Shell Programming –II
Switch, Loops and Functions
Shell Programming-II
case $variable-name in
pattern1)
Statement(s) to be executed if pattern1 matches
;;
pattern2)
Statement(s) to be executed if pattern2 matches
;;
pattern3)
Statement(s) to be executed if pattern3 matches
;;
*)
Default condition to be executed
;;
esac
The $variable-name is compared against the patterns until a match is found. The shell then
executes all the statements up to the two semicolons that are next to each other. The default is *)
and its executed if no match is found. There is no maximum number of patterns, but the minimum
is one.
You must include ;; at the end of each command. The shell executes all the statements up to the
two semicolons that are next to each other. The esac is always required to indicate end of case
statement. This is similar to break in the C programming language.
Example:
FRUIT="kiwi"
case "$FRUIT" in
"apple") echo "Apple pie is quite tasty."
;;
"banana") echo "I like banana nut bread."
;;
"kiwi") echo "New Zealand is famous for kiwi."
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
;;
esac
Output:
New Zealand is famous for kiwi.
Example:
Create a shell script called rental.sh:
# if no command line arg given
# set rental to Unknown
if [ -z $1 ] // -z flag causes test to check whether a string is empty.
then
rental="*** Unknown vehicle ***"
elseif [ -n $1 ]// -n to test for non-null strings
then
# otherwise make first arg as a rental
rental=$1
fi
fi
chmod +x rental.sh
./rental.sh
./rental.sh jeep
./rental.sh enfield
./rental.sh bike
Explanation:
1. Here first we will check, that if $1(first command line argument) is not given then set value
of rental variable to "*** Unknown vehicle ***",
2. if value is given then set it to given value.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
for var in "$@" //$@ refers to all of a shell script's command-line arguments. $1 , $2 , etc.
do
echo $var //%s means, "insert the first argument, a string, right here
done
Break Statement:
Much like C or Java, shell has a break command, also. It can be used to break out of a loop.
Consider this example which stops printing command line arguments, when it gets to one whose
value is "quit":
Continue Statement:
Shell has a continue that works just like it does in C or Java.
continue
fi
echo “your choice was $var“
done
while [ condition ]
do
command1
command2
command3
..
....
Done
Loop is executed as long as given condition is true. For eg. Above for loop program can be written
using while loop as
Example:
$cat > nt1
#Script to test while statement
##
if [ $# -eq 0 ]
then
echo "Error - Number missing form command line argument"
echo "Syntax : $0 number"
echo " Use to print multiplication table for given number"
exit 1
fi
n=$1
i=1
while [ $i -le 10 ]
do
echo "$n * $i = `expr $i \* $n`"
i=`expr $i + 1`
done
Save it and try as
$ chmod +x nt1
$./nt1 7
Example:
$ rm myf && echo File is removed successfully || echo File is not removed
If file (myf) is removed successful (exist status is zero) then "echo File is removed successfully"
statement is executed, otherwise "echo File is not removed" statement is executed (since exist
status is non-zero)
Functions:
Functions enable you to break down the overall functionality of a script into smaller, logical
subsections, which can then be called upon to perform their individual task when it is needed.
Creating Functions:
To declare a function, simply use the following syntax:
function_name () {
list of commands
}
The name of your function is function_name, and that's what you will use to call it from elsewhere
in your scripts. The function name must be followed by parentheses, which are followed by a list
of commands enclosed within braces.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Example:
Following is the simple example of using function:
When you would execute above script it would produce following result:
$./test.sh
Hello World
$
Following is an example where we pass two parameters Zara and Ali and then we capture and
print these parameters in the function.
$./test.sh
Hello World Zara Ali
$
Instead if you want to just terminate execution of the function, then there is way to come out of a
defined function. Based on the situation you can return any value from your function using the
return command whose syntax is as follows:
return code
Here code can be anything you choose here, but obviously you should choose something that is
meaningful or useful in the context of your script as a whole.
Example:
Following function returns a value 1:
# Define your function here
Hello () {
echo "Hello World $1 $2"
return 10
}
# Invoke your function
Hello Zara Ali
# Capture value returned by last command
ret=$?
echo "Return value is $ret"
$./test.sh
Hello World Zara Ali
Return value is 10
$
Nested Functions:
One of the more interesting features of functions is that they can call themselves as well as call
other functions. A function that calls itself is known as a recursive function.
1. "$*" special parameter takes the entire list as one argument with spaces between.
2. "$@" special parameter takes the entire list and separates it into separate arguments.
Example:
We can write the shell script shown below to process an unknown number of command-line
arguments with either the $* or $@ special parameters:
for TOKEN in $*
do
echo $TOKEN
done
Task 1:
Write Script, using function and case statement to perform basic math operation as follows +
Addition, - Subtraction, x Multiplication, / Division. By taking the operator and numbers choice
from user and then execute the appropriate case.
Task2:
Write a shell program to generate Fibonacci series using loop up to the limit specified by user.
Sample Output:
Sample Output: 0 1 1 2 3 5
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING