Conditional Control Structures: Dr.T.Logeswari
Conditional Control Structures: Dr.T.Logeswari
Dr.T.Logeswari
TEST COMMAND
test expression Syntax
Or
[ expression ]
Numeric test
String test
4
Output: True : $?=0; False : $?=1
Unix Shell Programming - Forouzan 4
STRING COMPARISON
OPERATORS MEANING
• If then fi statement
• If then else fi statement
• If then elif else fi statement
• Case easc statement
If then fi statement
if conditional expression
then
true block
fi
$?= 0, if true
$?=1, if false
10
Find largest of two numbers
Clear
echo “ enter two number”
Read a b
large=$a
If [ $b –gt $large ]; then
Large=$b
fi
If then else fi statement
if conditional expression
then
true block
else
false block
fi
12
Leap year or not
echo enter a year
read year
x=`expr $year % 4`
If [ $x –eq 0 ]
Then
echo $year is a leap year
else
echo $year is not leap year
fi
Odd or Even
clear
echo enter a number
read n
if [expr $num % 2` -eq 0]
then
echo n is a even number
else
echo n is not a even number
fi
What is wrong with this interactive
shell script?
echo What month is this?
read $month
echo $month is as good a month as any.
• In a file word UNIX is appearing many times?
How will you count number?
grep -c "Unix" filename
Write a script that will show the following as
output:
Give me a U!
U!
Give ma a N!
N!
Give me a I!
I!
Give me a X!
X!
for i in U N I X
if [ condition1 ]; then
statement1
elif [ condition2 ]; then
statement2
elif [ condition3 ]; then
statement3
else
default_statement
fi
itself
Find whether a number is positive,
negative or zero
echo enter a number
Read num
if [ $num –gt 0 ]; then
echo $num is positive
elif [ $num –lt 0 ]; then
echo $num is negative
elif [ $num –eq 0 ]; then
echo $num is zero
else
echo kindly enter a valid input
fi
case esac statement
• Used for a decision that is based on multiple choices
• Syntax:
case value in
pattern1) command-list1
;;
pattern2) command-list2
;;
patternN) command-listN
;;
*) default-list
24
;;
esac
• The value is compared against the patterns
until a match is found
• The case statement starts with the keywords
case and ends with the keyword easc
• Block of commands attached to every pattern
must be terminated with double semicolon(;;)
but not compulsory with default pattern
• The default *) pattern gets executed when no
match is found
• Case patterns (label) can be in any order
Unix commands using case case $ch in
statement
1) ls
1) display list of files
2) display todays date
;;
3) display calendar 2) date
4) display logged user ;;
5) display current directory 3)cal
6) quit ;;
4) who
echo menu ;;
echo 1.list of files 5)pwd
echo 2.todays date ;;
echo 3.display month of 6) exit
calender ;;
echo 4.logged user *) echo invalid choice
echo 5.display current ;;
directory
echo 6.quit
esac
echo"enter the choice" •
read ch
Looping control structures
• Loops are required whenever a set of
statement must be executed repeatedly
• The repeated execution also need decision
making to terminate the loop
• The three types of looping are
– while loop
– for loop
– until loop
while loop
To execute commands in “command-list” as long as
“expression” evaluates to true
Syntax:
while [ expression ]
do
command-list
done
28
Sum of digits
clear
sum=0
echo "enter a number"
read num
n=$sum
while [ $num -gt 0 ]
do
rem=`expr $num % 10`
sum=`expr $sum + $rem`
num=`expr $num / 10`
done
echo the sum of digit of $n is $sum
EXAMPLE: Using while loop
COUNTER=0
while [ $COUNTER -lt 10 ]
do
echo $COUNTER
let COUNTER + =1
done
30
UNTIL LOOP
• Purpose:
To execute commands in “command-list” as long as
“expression” evaluates to false
Syntax:
until [ expression ]
do
command-list
done
31
EXAMPLE: USING THE UNTIL LOOP
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]
do
echo $COUNTER
let COUNTER - =1
done
32
THE FOR LOOP
• Purpose:
To execute commands as many times as the number of
words in the “argument-list”
Syntax:
for variable in argument-list
do
commands
done
33
EXAMPLE 1: THE FOR LOOP
#!/bin/bash
for i in 7 9 2 3 4 5
do
echo $i
done
34
Jumping control structures
• Break
– The break statement is used to exit from a loop
structure based on certain condition
– The break statement cannot exit from nested
loops, it can exit only from the loop containing it
– Syntax:
break
• Continue
– The continue statement is used to skip the rest of
the statement in a loop and the execution
proceeds directly to the next iteration of the loop
– Syntax
continue
• exit
– The exit statement is used to terminate a program
– Syntax
exit