shell programs based on Control
Structures
Syntax:
if
[
expression ]
then
Statement(s)
to be executed if
expression is
true
fi
Description:-
The if...fi statement
is the fundamental control statement that allows Shell to make
decisions and execute statements conditionally.
If expression is
a shell command then it would be assumed true if it return 0 after
its execution. If it is a boolean expression then it would be true if
it returns true.
Example:
#!/bin/sh
a=10
b=20
if
[
$a ==
$b ]
then
echo
"a
is equal to b"
fi
if
[
$a !=
$b ]
then
echo
"a
is not equal to b"
fi
This
will produce following result:
a
is
not
equal to b
program:
#!/bin/bash
echo -n " enter a value :"
read a
echo -n " enter b value :"
read b
if [ $a -gt $b ]
then
echo "$a is greater then $b"
fi
if [ $a -lt $b ]
then
echo "$a is less then $b"
fi
if [ $a -ge $b ]
then
echo "$a is greater then or equal to $b"
fi
if [ $a -le $b ]
then
echo "$a is less then oe equal to $b"
fi
if [ $a -eq $b ]
then
echo "$a is equal to $b"
fi
if [ $a -ne $b ]
then
echo "$a is not equal to $b"
fi
Output:
Syntax:
if
[
expression ]
then
Statement(s)
to be executed if
expression is
true
else
Statement(s)
to be executed if
expression is
not
true
fi
Description:-
The if...else...fi statement
is the next form of control statement that allows Shell to execute
statements in more controlled way and making decision between two
choices.
Example:
If
we take above example then it can be written in better way
using if...else statement
as follows:
#!/bin/sh
a=10
b=20
if
[
$a ==
$b ]
then
echo
"a
is equal to b"
else
echo
"a
is not equal to b"
fi
This
will produce following result:
a
is
not
equal to b
Program:
#!/bin/bash
echo -n " enter a value :"
read a
echo -n " enter b value :"
read b
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal then b"
fi
Output:
Syntax:
if
[
expression 1
]
then
Statement(s)
to be executed if
expression 1
is
true
elif
[
expression 2
]
then
Statement(s)
to be executed if
expression 2
is
true
elif
[
expression 3
]
then
Statement(s)
to be executed if
expression 3
is
true
else
Statement(s)
to be executed if
no
expression is
true
fi
Description:-
The if...elif...fi statement
is the one level advance form of control statement that allows Shell
to make correct decision out of several conditions.
Example:
#!/bin/sh
a=10
b=20
if
[
$a ==
$b ]
then
echo
"a
is equal to b"
elif
[
$a -gt
$b ]
then
echo
"a
is greater than b"
elif
[
$a -lt
$b ]
then
echo
"a
is less than b"
else
echo
"None
of the condition met"
fi
This
will produce following result:
a
is
less than b
program:
#!/bin/bash
echo " enter a value:"
read a
echo " enter b value:"
read b
echo " enter c value:"
read c
if [ $a -gt $b -a $a -gt $c ]
then
echo "a is big"
elif [ $b -gt $c ]
then
echo "b is big "
else
echo "c is big"
fi
output:
Syntax:
The
basic syntax of the case...esac statement is to give an expression to
evaluate and several different statements to execute based on the
value of the expression.
The
interpreter checks each case against the value of the expression
until a match is found. If nothing matches, a default condition will
be used.
case
word 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
;;
esac
Description:-
Shell
support case...esac statement
which handles exactly this situation, and it does so more efficiently
than repeated if...elif statements.
Example:
#!/bin/sh
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."
;;
esac
This
will produce following result:
New
Zealand
is
famous for
kiwi.
Example
verified in ubuntu :
echo
"enter 1st value"
read
x
echo
"enter 2nd value"
read
y
echo
"enter 1 adding"
echo
"enter 2 sub"
echo
"enter 3 mul"
echo
"enter 4 quo"
echo
"enter 5 rem"
echo
"enter 6 exit"
echo
"enter * invalid number"
echo
"enter u r choice"
read
s
case
$s in
1)p=`expr
$x + $y`
echo
"sum =$p"
;;
2)p=`expr
$x - $y`
echo
"sub=$p"
;;
3)p=`expr
$x \* $y`
echo
"mul=$p"
;;
4)p=`expr
$x / $y`
echo
"quo =$p"
;;
5)p=`expr
$x % $y`
echo
"rem=$p"
;;
6)exit
;;
*)echo
"wrong choice"
;;
esac
program:
#!/bin/bash
echo "enter 1st value"
read x
echo "enter 2nd value"
read y
echo "enter 1 adding"
echo "enter 2 sub"
echo "enter 3 mul"
echo "enter 4 quo"
echo "enter 5 rem"
echo "enter 6 exit"
echo "enter * invalid number"
echo "enter u r choice"
read s
case $s in
1)p=`expr $x + $y`
echo "sum =$p"
;;
2)p=`expr $x - $y`
echo "sub=$p"
;;
3)p=`expr $x \* $y`
echo "mul=$p"
;;
4)p=`expr $x / $y`
echo "quo =$p"
;;
5)p=`expr $x % $y`
echo "rem=$p"
;;
6)exit
;;
*)echo "wrong choice"
;;
esac
output:
Syntax:
while
command
do
Statement(s)
to be executed if
command is
true
done
Description:-
Here Shell command is
evaluated. If the resulting value is true,
given statement(s) are
executed. If command is false then
no statement would be not executed and program would jump to the next
line after done statement
Example:
Here
is a simple example that uses for loop to span through the given list
of numbers:
#!/bin/sh
a=0
while
[ $a -lt
10 ]
do
echo
$a
a=`expr
$a + 1`
done
This
will produce following result:
0
1
2
3
4
5
6
7
8
9
program:
#!/bin/bash
echo "enter any number"
read n
fact=1
while [ $n -ne 0 ]
do
fact=`expr $fact \* $n`
n=`expr $n - 1`
done
echo "factorial is : $fact"
output:
Syntax:
for
var
in
word1 word2 ...
wordN
do
Statement(s)
to be executed for
every word.
done
Description:-
Here var is
the name of a variable and word1 to wordN are sequences of characters
separated by spaces (words). Each time the for loop executes, the
value of the variable var is set to the next word in the list of
words, word1 to wordN.
Example:
Here
is a simple example that uses for loop to span through the given list
of numbers:
#!/bin/sh
for
var
in
0
1
2
3
4
5
6
7
8
9
do
echo
$var
done
This
will produce following result:
0
1
2
3
4
5
6
7
8
9
program:
#!/bin/bash
fact=1
echo "enter a number"
read n
for (( i=$n; i>=1; i-- ))
do
fact=`expr $fact \* $i`
done
echo "the factorial of $n is $fact"
output: