Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 29

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH

Name : A V N L Sarojini
Designation : Lecturer
Branch : Computer Engineering
Institute : A.A.N.M. & V.V.R.S.R. Poly.,
Gudlavalleru.
Year/Semester : III Semester
Subject : UNIX & C
Subject Code : CM-302
Topic : Shell Programming &
Filtering Techniques
Duration : 50 Min
Sub Topic : Conditional Statements
Teaching Aids : PPTs.

CM304.14 1
Recap

 We can perform 3 types of tests using test command

1. Numeric Tests

2. String Tests

3. File Tests

CM304.14 2
Objectives

On completion of this period you would be able to


know
 The if-then-elif-else-fi statement
 if-then-elif-else-fi usage
 The case-esac statement
 case-esac statement usage
 Example programs
CM304.14 3
The if-then-elif-else-fi statement

 Syntax: if test_expression
then
command(s)
elif test_expression
then
command(s)
else
command(s)
fi

CM304.14 4
The if-then-elif-else-fi statement

 This form of if allows multiway branching.

 In this form of the if conditional there can be one


or more elif blocks. However the last one will be
an else block.

CM304.14 5
The if-then-elif-else-fi statement Contd..

 First the test expression that follows the keyword if is


evaluated first.

 If it returns a true, then the command(s) between then


and the first elif are executed and the control goes
beyond fi.

CM304.14 6
The if-then-elif-else-fi statement Contd..

 If the evaluation of the test expression following if


returns a false, then test expression following the first
elif is evaluated.

 If this evaluation returns true then command(s) between


then and next else are executed and the control goes
beyond fi.

CM304.14 7
The if-then-elif-else-fi statement Contd..

 If both if and all elifs result in false then command(s)


following else are executed.

 Thus only one of the blocks is executed always.

CM304.14 8
Example shell script using if-then-elif-else-fi

 Write a program to find the biggest number among the


three numbers.
echo “Enter Three numbers”
read a
read b
read c

CM304.14 9
Example shell script using if-then-elif-else-fi
Contd..

if [ $a -gt $b -a $a -gt $c ]
then
echo “$a is big”
elif [ $b -gt $a -a $b -gt $c ]
then
echo “$b is big”
else
echo “$c is big”
fi
fi

CM304.14 10
The case-esac statement

 This command provides the multi-way decision -


making facility. Basically it works on pattern
matching. In other words it works only with string
tests.

CM304.14 11
The case-esac statement syntax

case string_value in
pattern1)command
command
----------
command;;

CM304.14 12
Contd..
The case-esac statement syntax

pattern2) command
command
----------
command;;
---------
---------
*) echo “None of the Pattern Matched”;;
esac

CM304.14 13
The case-esac statement

 Every block of commands associated with a pattern


must be terminated by a double semi-colon
characters, that is ;;

 The case statement ends with the keyword esac, the


reverse of case.

CM304.14 14
The case-esac statement
 The string value that appears immediately after the
keyword case is compared in turn against each
pattern.

 As soon as a match is found, all commands


following that pattern till the immediate next
double semi-colon ;; are executed and then the
control goes beyond the esac.

CM304.14 15
The case-esac statement Contd..

 Case patterns can be constructed using wildcard


characters such as *, ? and others.

 One can use a default pattern with just a * character


as the last pattern within the scope of a case
statement.

CM304.14 16
The case-esac statement example

$cat menu.sh
echo “ MENU\n
1. List of files\n2.Todays date\n3. Process
status\n4. User of the system\n 5. PWD\c”
read choice

CM304.14 17
The case-esac statement example Contd..

case “$choice” in
1) ls -l;;
2) date;;
3) ps;;
4) who;;
5) pwd;;
*) echo “Invalid Choice”
exit;;
esac
$

CM304.14 18
The case-esac statement example
echo Enter any Character
read char
case $char in
[a-z])echo you have entered small character;;
[A-Z])echo you entered a Capital letter;;
[0-9])echo you entered a digit
?) echo you entered a special symbol
*) echo you entered more than one character
esac
CM304.14 19
Quiz

1. Case statement will end with

CM304.14 20
Quiz

1. Case statement will end with

CM304.14 21
Quiz

1. Case statement will end with

(A) esac

CM304.14 22
Quiz

1. Case statement will end with

(A) esac

CM304.14 23
Quiz

2.Shell has program language capabilities


[true/false]

CM304.14 24
Quiz

2.Shell has program language capabilities


[true/false]

CM304.14 25
Summary

At the end of this class, you have learnt

2. if-then-elif-elseif statements.

3. case statement

CM304.14 26
Assignment

 Write a shell script which receives any year from the


keyboard and determines whether the year is a leap
year or not.

CM304.14 27
Assignment

 Write a menu driven program which has following


options:

1. Contents of /etc/password
2. List of users who have currently logged in
3. Present working directory
4. Exit

CM304.14 28
Frequently asked questions

1. Explain the various conditional statements available


in shell.
2. With the help of a program, explain about
case..esac.

CM304.14 29

You might also like