Conditional Statement (IF) in Shell Script

Bash expression is the combination of operators, features, or values used to form a bash conditional statement. Conditional expression could be binary or unary expression which involves numeric, string or any commands whose return status is zero when success.Bash expression is the combination of operators, features, or values used to form a bash conditional statement. Conditional expression could be binary or unary expression which involves numeric, string or any commands whose return status is zero when success.

The shell provides several commands that we can use to control the flow of execution in our program. These include:

  • if
  • exit
  • for
  • while
  • until
  • case
  • break
  • continue

if

The first command we will look at is if. The if command is fairly simple on the surface; it makes a decision based on a condition. The ifcommand has three forms:

if [ expression ]
then
   Statement(s) to be executed if expression is true
else
   Statement(s) to be executed if expression is not true
fi
# First form

if condition ; then
    commands
fi

In the first form, if the condition is true, then commands are performed. If the condition is false, nothing is done.

# Second form

if condition ; then
    commands
else
    commands
fi

In the second form, if the condition is true, then the first set of commands is performed. If the condition is false, the second set of commands is performed.

# Third form

if condition ; then
    commands
elif condition ; then
    commands
fi

In the third form, if the condition is true, then the first set of commands is performed. If the condition is false, and if the second condition is true, then the second set of commands is performed.

What is a “condition”?

It is a part of statement, base on which your computer will take decision.

Exit status

A properly written Unix application will tell the operating system if it was successful or not. It does this by means of an exit status. The exit status is a numeric value in the range of 0 to 255. A “0” indicates success; any other value indicates failure. Exit status provides two important features. First, it can be used to detect and handle errors and second, it can be used to perform true/false tests.

It is easy to see that handling errors would be valuable. For example, in our script we will want to look at what kind of hardware is installed so we can include it in our report. Typically, we will try to query the hardware, and if an error is reported by whatever tool we use to do the query, our script will be able to skip the portion of the script which deals with the missing hardware.

We can also use the exit status to perform simple true/false decisions.

There are many different ways that an conditional statement can be used. These are summarized here:

String Comparison Description
Str1 = Str2 Returns true if the strings are equal
Str1 != Str2 Returns true if the strings are not equal
-n Str1 Returns true if the string is not null
-z Str1 Returns true if the string is null
Numeric Comparison Description
expr1 -eq expr2 Returns true if the expressions are equal
expr1 -ne expr2 Returns true if the expressions are not equal
expr1 -gt expr2 Returns true if expr1 is greater than expr2
expr1 -ge expr2 Returns true if expr1 is greater than or equal to expr2
expr1 -lt expr2 Returns true if expr1 is less than expr2
expr1 -le expr2 Returns true if expr1 is less than or equal to expr2
! expr1 Negates the result of the expression
File Conditionals Description
-d file True if the file is a directory
-e file True if the file exists (note that this is not particularly portable, thus -f is generally used)
-f file True if the provided string is a file
-g file True if the group id is set on a file
-r file True if the file is readable
-s file True if the file has a non-zero size
-u True if the user id is set on a file
-w True if the file is writable
-x True if the file is an executable

Few Examples:

1. Comparison of Two Variables.

echo "Comparison of Two Variables"
echo "Enter Value of Variable a"
read a
echo "Enter Value of Variable b"
read b

if [ $a == $b ]
then
   echo "a is equal to b"
elif [ $a -gt $b ]
then
   echo "a is greater than b"
elif [ $a -lt $b ]
then
   echo "a is less than b"
else
   echo "None of the condition met"
fi
2. Comparison of String

if [ "$1" = "chill" ]
then
    echo "Chill ICE"
else
    echo "Not so Chill"
fi

3. Comparison of Two Variables using Elseif.

if [ "$1" = "chill" ]
then
    echo "Chill ICE"
elif [ "$1" = "neat" ]
then
    echo "Neat ICE"
else
    echo "Not Chill Nor Neat ICE"
fi
4. Checking the Type of Documents.

if [ -f "$1" ]
then
    echo "$1 is a file"
else
    echo "$1 is not a file"
fi
 

Leave a Reply

Your email address will not be published.