Tuesday, 20 December 2016

Bash Scripting Conditionals - if-then-else, if-elif-else, Nested if-else

Bash Script 'if-else' Statements - Conditional Statements are those which help us take certain decisive actions against certain different conditions. These statements execute different blocks of code, depending upon whether a condition (or a boolean expression) provided by the programmer evaluates to true or false. In this article, we will learn Conditional Statements, their different types, their syntax and example of each of them. Most of the part of this article is straight forward and self explanatory.

bash-if-elif-else

Here we go!

Types of Conditional Statements

Below are different types of Conditional statements those can be used in bash script.
  • Simple if statement
  • if-else statement
  • if-elif-else statement
  • Nested if statement
Let us now see each of them, with their syntax and examples.

1. Simple if Statement

In simple if statements, the block of code is executed if and only if the condition evaluates to 'True'. Otherwise, the block of code is skipped.

Syntax:

if [ condition ]
then

    -- Block of Commands --

fi

Example:

#!/bin/bash

echo "Enter an Even Number :"
read n

i=$(expr $n % 2)

if [ $i -ne 0 ]
then

    echo "Its not Even!"

fi

Result:

Enter an Even Number :
3
Its not Even!

Note : read statement used in above example, is used to get input from the user. In this case, the user input will be stored into a variable n.

2. if-else Statement

In if-else statements, if the condition evaluates to 'True', the block of commands under if-then, i.e. Block of Commands 1, is executed while Block of Commands 2 is skipped. If the condition evaluates to 'False', Block of Commands 1 is skipped and block of code under else, i.e. Block of Commands 2, is executed.

Syntax:

if [ condition ]
then

    -- Block of Commands 1 --

else

    -- Block of Commands 2 --

fi

Example:

#!/bin/bash

echo "Enter a Natural Number :"
read n

i=$(expr $n % 2)

if [ $i -eq 0 ]
then

    echo "Its Even!"

else

    echo "Its Odd!"

fi

Result:

Enter a Natural Number :
7
Its Odd!

Enter a Natural Number :
12
Its Even!

3. if - elif - else Statement

In case of if-elif-else statements, 'condition 1' is checked and if it evaluates to 'True', Block of Commands 1 is executed. If 'condition 1' evaluates to 'False', then 'condition 2' in elif block is checked. If it evaluates to 'True', then Block of Commands 2 is executed. If both the conditions are evaluated to 'False', then program jumps to the else part and executes Block of Commands 3.

Syntax:

if [ condition1 ]
then

    -- Block of Commands 1 --

elif [ condition2 ]
then

    -- Block of Commands 2 --

else

    -- Block of Commands 3 --

fi

Example:

#!/bin/bash

echo "Enter any Number :"
read n

if [ $n -lt 0 ]
then

    echo "Its Negative!"

elif [ $n -eq 0 ]
then

    echo "Its Neither Positive Nor Negative!!"

else

    echo "Its Positive!"

fi

Result:

Enter any Number :
20
Its Positive!

Enter any Number :
-5
Its Negative!

Enter any Number :
0
Its Neither Positive Nor Negative!


4. Nested if-else Statement

In nested if statements, there are one or more if-then-else or if-elif-else statements nested in one if-then-else or if-elif-else statement.

Syntax:

if [ condition ]
then

    -- Block of Commands --

else
    
    if [ condition ]
    then

        -- Block of Commands --
    fi

fi

Example:

#!/bin/bash

echo "Balance = $1000"
echo "Enter the Amount You want to Withdraw."
read a

if [ $a -gt 1000 ]
then

    echo "Insufficient Balance!"

else
    
    if [ $a -gt 950 ]
    then

        echo "Warning! Keep $50 in your account to keep it active!"
    fi

fi

Result:

Balance = $1000
Enter the Amount You want to Withdraw:
960
Warning! Keep $50 in your account to keep it active!

That's all about 'Conditional Statements'! After reading this article, one should be able to decide which statement must be used given a scenario and write those statements. Although very basic examples are provides, those should have given you a clear idea about how the control flow is switched depending on what the condition evaluates to. Thanks for reading this article, share your views and feedback in the comment section below and stay tuned.

0 comments:

Post a Comment