Debug School

rakesh kumar
rakesh kumar

Posted on

Bash Else If and Bash Case

Bash If

Basic if Statements
if [ $number -gt 125 ]
if [ "myfile" == "yourfile" ];

if [ 3 -lt 10 ];

if [ 8 -gt 7 ] || [ 10 -eq 3 ];

if [[ 10 -eq 10 && 5 -gt 4 || 3 -eq 4 || 3 -lt 6 ]];

-d FILE,-e FILE,-r FILE,-s FILE,-w FILE,x FILE,-z STRING,-n STRING
! EXPRESSION

Bash If Else
if [ 10 -gt 3 ];
if [[ 10 -gt 9 && 10 == 9 || 2 -lt 1 || 25 -gt 20 ]];

if [[ 10 -gt 9 && 10 == 8 || 3 -gt 4 || 8 -gt 8 ]];

Bash If Else Statement in a Single Line
read -p "Enter a value:" value

if [ $value -gt 9 ]; then echo "The value you typed is greater than 9."; else echo "The value you typed is not greater than 9."; fi

In this topic, we will understand how to use else-if (elif) statements in Bash scripts to get our automated tasks completed.

Bash else-if statement is used for multiple conditions. It is just like an addition to Bash if-else statement. In Bash elif, there can be several elif blocks with a boolean expression for each one of them. In the case of the first 'if statement', if a condition goes false, then the second 'if condition' is checked.

Syntax of Bash Else If (elif)
The syntax of else-if statement in Bash shell scripting can be defined as:

if [ condition ];  
then  
<commands>  
elif [ condition ];  
then  
<commands>  
else  
<commands>  
fi  
Enter fullscreen mode Exit fullscreen mode

Just like if-else, we can use a set of one or more conditions joined using conditional operators. The set of commands are executed when the condition is true. If there is no true condition, then the block of commands inside the 'else statement' is executed.

Following are some examples demonstrating the usage of the else-if statement:

Example 1
Following example consists of two different scenarios wherein the first else-if statement, the condition is true, and in the second else-if statement, the condition is false.

Bash Script

!/bin/bash

read -p "Enter a number of quantity:" num  

if [ $num -gt 100 ];  
then  
echo "Eligible for 10% discount"  
elif [ $num -lt 100 ];  
then  
echo "Eligible for 5% discount"  
else  
echo "Lucky Draw Winner"  
echo "Eligible to get the item for free"  
fi  
Enter fullscreen mode Exit fullscreen mode

Output

If we enter the number of quantity as 110, then the condition of 'if statement' evaluates to true and the output looks like:

Image description

If we enter the number of quantity as 90 then condition of 'elif statement' evaluates to true, and the output looks like:

Image description

If we enter the number of quantity as 100, then no condition will be true. In this case, the block of commands inside the 'else statement' is executed, and the output looks like:

Image description

This is how basic bash else-if works.

Example 2
This example is demonstrating how to use multiple conditions with the else-if statement in Bash. We use bash logical operators to join multiple conditions.

Bash Script

#!/bin/bash  

read -p "Enter a number of quantity:" num  

if [ $num -gt 200 ];  
then  
echo "Eligible for 20% discount"  

elif [[ $num == 200 || $num == 100 ]];  
then  
echo "Lucky Draw Winner"  
echo "Eligible to get the item for free"  

elif [[ $num -gt 100 && $num -lt 200 ]];  
then  
echo "Eligible for 10% discount"  

elif [ $num -lt 100 ];  
then  
echo "No discount"  
fi  
Enter fullscreen mode Exit fullscreen mode

Note: It should be noted that else block is optional.
Output

If we enter the number of quantity as 100, then the output will look like:

Image description

Bash Case
In this topic, we will discuss the basics of case statements and how to use them in Bash scripts.

The Bash case statement is the simplest form of IF-THEN-ELSE with many ELIF elements. Using the case statement makes our bash script more readable and easier to maintain. These are generally applied to simplify the complex conditions having multiple different choices.

The Bash case statement follows a similar logic as the Javascript or C switch statement. There is a slight difference, as follows:

The Bash case statement takes a value once and tests that value multiple times. It stops searching for a pattern once it has found it and executed the statement linked with it, which is almost opposite in case of the C switch statement.
Case Statement Syntax
Syntax of the bash case statement is given below:

case expression in  
    pattern_1)  
        statements  
        ;;  
    pattern_2)  
        statements  
        ;;  
    pattern_3|pattern_4|pattern_5)  
        statements  
        ;;  
    pattern-n)  
        statements  
        ;;  
    *)  
        statements  
        ;;  
Enter fullscreen mode Exit fullscreen mode

esac

There are some key points of bash case statements:

Each case statement in bash starts with the 'case' keyword, followed by the case expression and 'in' keyword. The case statement is closed by 'esac' keyword.
We can apply multiple patterns separated by | operator. The ) operator indicates the termination of a pattern list.
A pattern containing the statements is referred to as a clause, and it must be terminated by double semicolon (;;).
An asterisk symbol () is used as a final pattern to define the default case. It is used as a default case when used as the last case.
**How it works
*
First of all, the case statement expands the expression and tries to match with each of the included patterns. When it finds a match, all the linked statements are executed till the double semicolon (;;). After the first match, case terminates with the exit status of the last executed statement.

If there is no matched pattern, the exit status of the case is zero. Otherwise, the return status is the exit status of the executed statements.

If the default asterisk pattern is used, it will be executed in case of no matched pattern.

Let's try to understand this mechanism with the help of a few examples:

Example 1
In this example, we have defined a simple scenario to demonstrate the use of the case statement.

Bash Script

#!/bin/bash  

echo "Do you know Java Programming?"  
read -p "Yes/No? :" Answer  
case $Answer in  
    Yes|yes|y|Y)  
        echo "That's amazing."  
        echo  
        ;;  
    No|no|N|n)  
        echo "It's easy. Let's start learning from javatpoint."  
        ;;  
esac  
Enter fullscreen mode Exit fullscreen mode

Output

Image description

Top comments (0)