Monday, 5 August 2019

if condition in unix shell script

Unix Shell programming like other languages have the conditional statement processing also.if condition in unix shell script (Conditional statement) add intelligence to our scripts. This gives us the functionality to perform various command based on various conditions

We will be working on if condition in unix shell script (if/then/else statement) in this post
The basic format for the if/then/else conditional statement processing is
if condition
then
commands
else
commands
fi
A Example would be
if [ $name = “” ];
then
echo “name is empty”
else
echo “name is $name”
fi
It is also possible to create compound conditional statements by using one or more else if (elif) clauses. If the first condition is false, then subsequent elif statements are checked. When an elif condition is found to be true, the statements following the associated then statement are executed.
if condition
then commands
elif condition
then
commands
fi
Algorithm for if condition in unix shell script
The if statement uses the exit status of the given condition
if test condition
then
commands (if condition is true)
else
commands (if condition is false)
fiif statements may be nested:
if …
then …
else if …

fi
fi
Test on strings:  
The test on strings can be carried in below ways
[[ -z string ]]True if string is empty.
[[ -n string ]]True if string is not empty.
[[ string1 = string2 ]]True if string1 equals string2.
[[ string1 != string2 ]]True if string1 does not equal string2.
Test on objects :
The conditional  on files, directories, links  can be carried out in below ways
TestDescription
-bfile exists and is block special
-cfile exists and is character special
-dfile exists and is a directory
-efile exists
-ffile exists and is a regular file
-gfile exists and is set-group-ID
-Gfile exists and is owned by the effective group ID
-hfile exists and is a symbolic link (same as -L
-Lfile exists and is a symbolic link (same as -h)
-Ofile exists and is owned by the effective user ID
-pfile exists and is a named pipe
-rfile exists and is readable
-Sfile exists and is a socket
-sfile exists and has a size greater than zero
-wfile exists and is writable
-xfile exists and is executable
-ntfile1 is newer (modification date) than file2
-otfile1 is older than file2
-effile1 and file2 have the same device and inode numbers
Examples :
[[ -f $file ]] # is $myfile a regular file?
[[ -x /usr/users/apply ]] # is this file executable?file=input.out
if [ ! -f $file ]
then
echo “File $file not found”
exit 0
fi
if [ -e “.bash_profile” ]
then
echo ” The file exists ”
else
echo ” File not found ”
fi
Test for Number:
OperatorMeaningMathematical Equivalent
-eqequal tox == y
-gegreater than or equal tox >= y
-gtgreater thanx > y
-leless than or equal tox <= y
-ltless thanx < y
-nenot equal tox != y
And and OR operator
we can test multiple test condition with && and || operator
&& – this stand for and condition
[[ $1 == yes && -r $1.txt ]]
So both the condition need to be true for this whole expression to be true
|| – This stand for or condition
[[ $1 == yes || -r $1.txt ]]
Only one condition need to be true for this whole expression to be true

0 comments:

Post a Comment