0% found this document useful (0 votes)
12 views

Control Structures

The document discusses control structures and loops in Unix Shell. It describes if-else statements, case statements, and different types of loops (while, for, until, select) used to control program flow and execute commands conditionally or repeatedly. If-else statements allow executing one set of commands if a condition is true and optionally another set if false. Case statements allow choosing different code blocks based on the value of a variable. Loops like while and for repeat commands, while until repeats until a condition is true. Select creates a menu to choose options from.

Uploaded by

NITIN SHUKLA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Control Structures

The document discusses control structures and loops in Unix Shell. It describes if-else statements, case statements, and different types of loops (while, for, until, select) used to control program flow and execute commands conditionally or repeatedly. If-else statements allow executing one set of commands if a condition is true and optionally another set if false. Case statements allow choosing different code blocks based on the value of a variable. Loops like while and for repeat commands, while until repeats until a condition is true. Select creates a menu to choose options from.

Uploaded by

NITIN SHUKLA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

CONTROL

STRUCTURES
Unix Shell supports conditional statements which are used to
perform different actions based on different conditions.
two decision-making statements
 The if...else statement
 The case...esac statement
The if...else statements
If else statements are useful decision-making statements which can
be used to select an option from a given set of options.
Unix Shell supports following forms of if…else statement −
if...fi statement
if...else...fi statement
if...elif...else...fi statement
if...fi statement

 The if...fi statement is the fundamental control statement that


allows Shell to make decisions and execute statements
conditionally.
I f [ expression ]
then
Statement(s) to be executed if expression is true
fi
#!/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
 The if...else...fi statement is the next form of control statement that
allows Shell to execute statements in a controlled way and make the
right choice
if [ expression ]
Then
Statement(s) to be executed
if expression is true
else
Statement(s) to be executed if expression is not true
fi
#!/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
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.
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
CASE…ECASE
 You can use multiple if...elif statements to perform a multiway
branch.
 However, this is not always the best solution, especially when
all of the branches depend on the value of a single variable.
 Unix Shell supports case...esac statement which handles exactly
this situation, and it does so more efficiently than repeated
if...elif statements.
case word in
pattern1)
Statement(s) to be executed if pattern1 matches
;;
pattern2)
Statement(s) to be executed if pattern2 matches
;;
*)
Default condition to be executed
;;
esac
#!/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
LOOPS
A loop is a powerful programming tool that enables you to execute a set of
commands repeatedly
The while loop
The for loop
The until loop
The select loop
 The while loop enables you to execute a set of commands repeatedly until
some condition occurs. It is usually used when you need to manipulate the
value of a variable repeatedly.
SYNTAX
while command
do
Statement(s) to be executed if command is true
done
a=0
While
[ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
The for loop operates on lists of items. It repeats a set of commands for every
item in a list.
syntax
for var in word1 word2 ... wordN
do
Statement(s) to be executed for every word.
done
for var in 0 1 2 3 4 5 6 7 8 9
do
echo $var
done
The while loop is perfect for a situation where you need to execute
a set of commands while some condition is true. Sometimes you
need to execute a set of commands until a condition is true.
Syntax
until command
do
Statement(s) to be executed until command is true
done
a=0
until
[ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
The select loop provides an easy way to create a numbered menu
from which users can select options. It is useful when you need to
ask the user to choose one or more items from a list of choices.
Syntax
select var in word1 word2 ... wordN
Do
Statement(s) to be executed for every word.
done
select DRINK in tea cofee water juice appe all none
do
case $DRINK in
tea|cofee|water|all)
echo "Go to canteen”
;;
juice|appe)
echo "Available at home"
;;
none)
break
;;
*)
echo "ERROR: Invalid selection"
;;
esac
done

You might also like