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

if condition in unix shell script

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

Test Description
-b file exists and is block special
-c file exists and is character special
-d file exists and is a directory
-e file exists
-f file exists and is a regular file
-g file exists and is set-group-ID
-G file exists and is owned by the effective group ID
-h file exists and is a symbolic link (same as -L
-L file exists and is a symbolic link (same as -h)
-O file exists and is owned by the effective user ID
-p file exists and is a named pipe
-r file exists and is readable
-S file exists and is a socket
-s file exists and has a size greater than zero
-w file exists and is writable
-x file exists and is executable
-nt file1 is newer (modification date) than file2
-ot file1 is older than file2
-ef file1 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:

Operator Meaning Mathematical Equivalent
-eq equal to x == y
-ge greater than or equal to x >= y
-gt greater than x > y
-le less than or equal to x <= y
-lt less than x < y
-ne not equal to x != 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

Related Posts

What is Shell and Shell Scripts

All about awk command

If condition examples