Home » Unix command and scripts » if condition in shell script

if condition in shell script

If condition

Unix Shell scripting 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

if condition in unix shell script

We will be working on if condition in 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)
fi

 if statements may be nested:
if ...
then ...
else if ...
...
fi
fi

 

Comparison 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

See also  How to move a file/directory in Linux with Example
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 ]]

See also  sed delete line containing string

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

Related Posts

Shell and Shell Scripts
awk command in Unix
If condition examples
sed command

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top