Shell Programmingifelse String File Part4
Shell Programmingifelse String File Part4
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.
Syntax
if [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if expression is not true
fi
The Shell expression is evaluated in the above syntax. If the resulting value is true, given statement(s) are
executed. If the expression is false, then no statement will be executed.
Example
The above example can also be written using the if...else statement as follows −
Live Demo
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
else
fi
-eq Checks if the value of two operands are [ $a -eq $b ] is not true.
equal or not; if yes, then the condition
becomes true.
It is very important to understand that all the conditional expressions should be placed inside square braces with
spaces around them. For example, [ $a <= $b ] is correct whereas, [$a <= $b] is incorrect.
Example
Here is an example which uses all the relational operators −
Live Demo
#!/bin/sh
a=10
b=20
if [ $a -eq $b ]
then
else
if [ $a -ne $b ]
then
else
fi
if [ $a -gt $b ]
then
else
fi
if [ $a -lt $b ]
then
fi
if [ $a -ge $b ]
then
else
fi
if [ $a -le $b ]
then
else
fi
Boolean Operators
The following Boolean operators are supported by the Bourne Shell.
Show Examples
a=10
b=20
if [ $a != $b ]
then
else
fi
then
else
fi
if [ $a -lt 100 -o $b -gt 100 ]
then
else
fi
then
else
fi
String Operators
The following string operators are supported by Bourne Shell.
Show Examples
#!/bin/sh
a="abc"
b="efg"
if [ $a = $b ]
then
else
fi
if [ $a != $b ]
then
else
echo "$a != $b: a is equal to b"
fi
if [ -z $a ]
then
else
fi
if [ -n $a ]
then
else
fi
if [ $a ]
then
echo "$a : string is not empty"
else
fi
Assume a variable file holds an existing file name "test" the size of which is 100 bytes and
has read, write and execute permission on −
We have a few operators that can be used to test various properties associated with a Unix file.
Assume a variable file holds an existing file name "test" the size of which is 100 bytes and
has read, write and execute permission on −
-d file Checks if file is a directory; if yes, then the [ -d $file ] is not true.
condition becomes true.
-g file Checks if file has its set group ID (SGID) bit [ -g $file ] is false.
set; if yes, then the condition becomes true.
-k file Checks if file has its sticky bit set; if yes, then [ -k $file ] is false.
the condition becomes true.
-p file Checks if file is a named pipe; if yes, then the [ -p $file ] is false.
condition becomes true.
-s file Checks if file has size greater than 0; if yes, [ -s $file ] is true.
then condition becomes true.
Assume a variable file holds an existing file name "/var/www/tutorialspoint/unix/test.sh" the size of which
is 100 bytes and has read, write and execute permission −
Live Demo
#!/bin/sh
file="/var/www/tutorialspoint/unix/test.sh"
if [ -r $file ]
then
els e
fi
if [ -w $file ]
then
else
fi
if [ -x $file ]
then
else
fi
if [ -f $file ]
then
else
fi
if [ -d $file ]
then
else
fi
if [ -s $file ]
then
else
fi
if [ -e $file ]
then
else
echo "File does not exist"
fi