Bash if else Statment

simple 4 types of the bash else if like as a if statement, if-else statement, else-if ladder statement and nested if statement.

Bash if else Statment

If-else is the custom decision making easy to check condition statements in bash scripting other to any other like as c, c++ java or php programming.

Syntax:

if [condition]
then
    //if block code
else
   // else block code
fi

: Related Query :

if Statement Example

#!/bin/bashs
 
read -p "Enter numeric value: " myvar
 
if [ $myvar -gt 10 ]
then
    echo "Value is greater than 10"
fi

if-else Statement Example

#!/bin/bashs
 
read -p "Enter numeric value: " myvar
 
if [ $myvar -gt 10 ]
then
    echo "OK"
else
    echo "Not OK"
fi

If-elif-else Statement Example

#!/bin/bashs
 
read -p "Enter your marks: " marks
 
if [ $marks -ge 80 ]
then
    echo "Very Good"
 
elif [ $marks -ge 50 ]
then
    echo "Good"
 
elif [ $marks -ge 33 ]
then
    echo "Just Satisfactory"
else
    echo "Not OK"
fi

Also Read: if else statement using Bash Scripting

Read Also:  ng-keypress Event using Angular Example

Nested if Statement Example

#!/bin/bash
 
read -p "Enter value of i :" i
read -p "Enter value of j :" j
read -p "Enter value of k :" k
 
if [ $i -gt $j ]
then
    if [ $i -gt $k ]
    then
        echo "i is greatest"
    else
        echo "k is greatest"
    fi
else
    if [ $j -gt $k ]
    then
        echo "j is greatest"
    else
 echo "k is greatest"
    fi
fi