Você está na página 1de 28

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.13 1
Objective
On completion of this period you would be able
to know
 Introduction to conditional tests
 Using test command
 Using test command for numeric test
 Using test command for string test
 Using test command for file test
 if-then-fi conditional statement
 if-then-else-fi conditional statement

CM304.13 2
What is Conditional Test?

 By default the instructions in the program are executed


sequentially. Most of the times you need the program to
do one thing under some condition and a different thing
in another situation.

 For this it is essential to have control over the order of


execution of commands in a program.

CM304.13 3
Various conditional statements in shell

 The UNIX shell offers 4 decision making


instructions, they are:

(a) The if-then-fi statement


(b) The if-then-else-fi statement
(c) The if-then-elif-else-fi statement
(d) The case-esac statement

CM304.13 4
test command

 This is a built in shell command that evaluates the


expression given to it as an argument.

test command returns true if the evaluation of the


expression returns a zero (0).

test command returns false if the evaluation return


non-zero.

CM304.13 5
test command Contd..

 This command can be used directly using the


keyword test (or) indirectly using the square
brackets.

 Example.
$if test ”$answer”=“y”
(or)
$if [“$answer”=“y”]

CM304.13 6
test command Contd..

 More than one condition can be tested by connecting


them together using logical operators such as

-a (Logical AND operator)


-o (Logical OR operator)

! (Logical NOT operator)

CM304.13 7
test command

 With test command, the following types of tests are


carried out.

1. Numeric Tests
2. String Tests
3. File Tests

CM304.13 8
test command - Numeric Tests

 In Numeric tests, two numbers are compared using


relational operators that are listed below
Operator Meaning

-eq Equal to
-ne Not Equal to
-gt Greater than
-ge Greater than or equal to
-lt Less than
-le Less than or equal to
CM304.13 9
test command -Numeric Tests (Example)

 $x=5;y=7
 $test $x -eq $y

(or)

 $x=5;y=7
 $[ $x -lt $y ]

CM304.13 10
test command - String Tests

 String tests are conducted for checking.


 Equality
 Non – equality
 Zero or non- zero

CM304.13 11
test command - String Tests

 Following are list of string tests


String Tests Meaning
True if length of the string is zero, that is, the string is
-z string
null.
True if length of the string is nonzero, that is, if string
-n string
exists.
string1 = string 2 True if string1 and string2 are identical.

string1 != string2 True of string1 and string2 are not identical.

string1 True if string1 is not the null string.

CM304.13 12
test command -String Tests (Example)

 $ans=y
 [ “$ans”=“y” ]
 [ “$ans”!=“y” ]
 [ -n “$ans” ]
 [ -z “$ans” ]

CM304.13 13
test command - File tests

 File tests are conducted for checking the status of


files and directories.

Test Exit Status


-e file True if file exits
-f file True if file exists and is a regular file
-r file True if file exists and is readable.
-w file True if file exists and is writable.
-x file True if file exists and is executable.
-d file True if file exits and is a directory.

CM304.13 14
test command - File tests

 File tests are conducted for checking the status of


files and directories.

Test Exit Status


-c file True if file exists and is a character special file.
-b file True if file exists and is a block special file.
-h file True if file exists and is a link file.
-s file True if file exists and has a size greater than zero.

CM304.13 15
test command - File tests ( Example)

 $[ -f student.lst ]

 $[ -x student.lst ]

CM304.13 16
The if-then-fi statement

 Syntax:
if test_expression
then
true_block
statement 1
- - -
- - -
statement n
fi

CM304.13 17
The if-then-fi statement

 It evaluates the test expression that appears along the


keyword if first.

 If this evaluation result in a true, then the commands


in the true_block are executed.

 Otherwise the true_block between then and fi is


skipped.

CM304.13 18
Example shell script using if-then-fi

echo “Enter a number”


read n
if [ $n -le 0 ]
then
echo “not a positive no”
echo “Enter a positive no”
fi

CM304.13 19
Example2

 Write shell script to find largest among two numbers.


echo –n “ enter two numbers”
read value1 value2
big = $ value1;
if [ $value1<$value2] then
big= $value2;
fi
echo “ the biggest number is $big”

CM304.13 20
The if-then-else-fi statement

 Syntax:
if test_expression
then
true_block
else
false_block
fi

CM304.13 21
The if-then-else-fi statement

 If test_expression results in a true then the true_block will


be executed and then the control goes beyond fi.

 If the evaluation of the test_expression results in a false


then the false_block will be executed and the control goes
beyond fi.

CM304.13 22
Example shell script using if-then-else-fi

echo “Enter Student marks in 5 subjects”


read a
read b
read c
read d
read e

CM304.13 23
Example shell script using if-then-else-fi

sum=`expr $a + $b + $c + $d + $e`
avg=`expr $sum / 5`
if [ $avg -ge 60]
then
echo “Student secured First division”
else
echo “Student secured Second Division”
fi

CM304.13 24
Example2
 Write shell script to check whether the number is
even or not.
echo –n “ enter number”
read number
check= `expr $number %2`
if [ $check –eq 0]
then
echo “ the number is even”
else
echo “ the number is odd”
fi
CM304.13 25
Summary

In this class, you have learnt


Conditional tests available in shell programming.

CM304.13 26
Quiz

1.What will be the output of the below program

x=10
if [ $x -ge 2 ]
echo $x
fi

output is 10, since conditon is true

CM304.13 27
Quiz
2.What will be the output of the below program
x=3
y=3.0
if [ $x -eq $y ]
then
echo “ X and Y and equal”
else
echo “X and Y and not equal’
fi
Output
X and Y are equal
CM304.13 28

Você também pode gostar