Professional Documents
Culture Documents
CM304
CM304
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
1. Numeric Tests
2. String Tests
3. File Tests
CM304.14 2
Objectives
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
CM304.14 5
The if-then-elif-else-fi statement Contd..
CM304.14 6
The if-then-elif-else-fi statement Contd..
CM304.14 7
The if-then-elif-else-fi statement Contd..
CM304.14 8
Example shell script using if-then-elif-else-fi
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
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
CM304.14 14
The case-esac statement
The string value that appears immediately after the
keyword case is compared in turn against each
pattern.
CM304.14 15
The case-esac statement Contd..
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
CM304.14 20
Quiz
CM304.14 21
Quiz
(A) esac
CM304.14 22
Quiz
(A) esac
CM304.14 23
Quiz
CM304.14 24
Quiz
CM304.14 25
Summary
2. if-then-elif-elseif statements.
3. case statement
CM304.14 26
Assignment
CM304.14 27
Assignment
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
CM304.14 29