Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks



Thursday, 10 October 2013

'Simple If', 'If-else', 'If-elif-else' and 'Nested If' Statements in Bash Scripting

    Conditional Statements are those which execute different blocks of code depending upon whether a Boolean expression provided by the programmer evaluates to a true condition or a false one. In this article, we will learn Conditional Statements, its different types, their syntax and example of each of them. We have already seen these statements in some of our previous articles on Basic Linux Shell Scripting Language. So, most of the part of this article is self explanatory.


Here we go!

Types of Conditional Statements

  • Simple If Statement
  • 'If-else' Statement
  • 'If-elif-else' Statement
  • 'Nested If' Statement

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!

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. Otherwise, '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 'true', 'Block of Commands 1' is executed. If condition 1 evaluates to be false, then condition 2 in 'elif' is checked, if it evaluates to be 'true', then 'Block of Commands 2' is executed. If both the conditions are evaluated to be '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' 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'!

Also Read:

0 comments:

Post a Comment