Conditional Statement (CASE)

 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 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 expression in  
    pattern_1)  
        statements  
        ;;  
    pattern_2)  
        statements  
        ;;  
    pattern_3|pattern_4|pattern_5)  
        statements  
        ;;  
    pattern-n)  
        statements  
        ;;  
    *)  
        statements  
        ;;  
esac


  •  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
-----------------------------------------------------------------------------------------------------------------------------
Example 1
-----------------------------------------------------------------------------------------------------------------------------

-- Script

[haritaraka12078@cxln4 ~]$ cat>case1.sh
#!/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


[haritaraka12078@cxln4 ~]$ sh case1.sh
Do you know Java Programming?
Yes/No? :y
That's amazing.
[haritaraka12078@cxln4 ~]$ sh case1.sh
Do you know Java Programming?
Yes/No? :no
It's easy. Let's start learning from javatpoint.


-----------------------------------------------------------------------------------------------------------------------------
Example 
-----------------------------------------------------------------------------------------------------------------------------

-- Script
[haritaraka12078@cxln4 ~]$ cat>case2.sh
#!/bin/bash

echo "Which Operating System are you using?"
echo "Windows, Android, Chrome, Linux, Others?"

read -p "Type your OS Name:" OS

case $OS in
    Windows|windows)
        echo "That's common. You should try something new."
        echo
        ;;
    Android|android)
        echo "This is my favorite. It has lots of applications."
        echo
        ;;
    Chrome|chrome)
        echo "Cool!!! It's for pro users. Amazing Choice."
        echo
        ;;
    Linux|linux)
        echo "You might be serious about security!!"
        echo
        ;;
    *)
        echo "Sounds interesting. I will try that."
        echo
        ;;
esac


[haritaraka12078@cxln4 ~]$ sh case2.sh
Which Operating System are you using?
Windows, Android, Chrome, Linux, Others?
Type your OS Name:chrome
Cool!!! It's for pro users. Amazing Choice.


[haritaraka12078@cxln4 ~]$ sh case2.sh
Which Operating System are you using?
Windows, Android, Chrome, Linux, Others?
Type your OS Name:
Sounds interesting. I will try that.

=======================================================================
[prabhucloudxlab@cxln4 ~]$ cat>hello.sh
#! bin/bash
# CASE statement
car=$1
case $car in 
"BMW" )
  echo "it is bmw car";;
  
"mercadese" )
echo "it is mercadese car";;

"toyota" )
  echo "it is toyota car";;
  
"honda" )
echo "it is honda car";;

*)
  echo "it is unknown car";;
esac



[prabhucloudxlab@cxln4 ~]$ sh hello.sh honda
it is honda car