Programming Tutorials

Nested if-else-fi in Linux Shell Script

By: Dorris in Linux Tutorials on 2011-01-15  

In Linux Shell Script, we can use nested if-else-fi statements to execute a set of statements based on multiple conditions.

Here is an example of nested if-else-fi in Linux Shell Script:

#!/bin/bash

a=10
b=20

if [ $a == $b ]
then
   echo "a is equal to b"
else
   if [ $a -gt $b ]
   then
      echo "a is greater than b"
   else
      echo "a is less than b"
   fi
fi

In this example, we first check if the variable a is equal to b. If not, we use a nested if-else-fi statement to check if a is greater than b or less than b and output the appropriate message.

Note that the inner if-else-fi statement must be enclosed in a pair of else and fi statements from the outer if-else-fi statement.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Linux )

Latest Articles (in Linux)