How to use Case Statement on Bash Script

To use case Statement on Bash Script ?

Introduction:

A case statement in bash scripts is used when a decision has to be made against multiple choices. In other words, it is useful when an expression has the possibility to have multiple values. This methodology can be seen as a replacement for multiple if-statements in a script. Case statements have an edge over if-statements because it improves the readability of our code and they are easier to maintain.

Installation Steps:

Step1: Check the Oracle Linux Version by using the below command

[root@linuxhelp ~]# cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="9.2"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="9.2"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Oracle Linux Server 9.2"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:9:2:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://github.com/oracle/oracle-linux"
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 9"
ORACLE_BUGZILLA_PRODUCT_VERSION=9.2
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=9.2

Step 2: Next create a one script file for Case Statement by using Vim command

[root@linuxhelp ~]# vim casestatement
#!/bin/bash
echo -n "Are you a Student  [Yes] or [No] : "
read response
case $response in

        "Y" | "y" | "yes" | "Yes" | "YES" )
        echo -n "Yes,I am a student"
        ;;
        "N" | "n" | "no" | "No" | "NO" )
        echo -n "No,I am not a student"
        ;;
        *)
        echo -n "Invalid Response"
        ;;
esac

Step 3: Execute the script first change the executable file permission by using the following command

[root@linuxhelp ~]# chmod +x casestatement

Step 4: List the file by using the following commands

[root@linuxhelp ~]# ll
total 8
-rw-------. 1 root root 1062 Sep  4 17:37 anaconda-ks.cfg
-rwxr-xr-x. 1 root root  345 Sep 25 16:47 casestatement

Step 5: Execute the script by using the following command

[root@linuxhelp ~]# ./casestatement
Are you a Student  [Yes] or [No] : y
Yes,I am a student[root@linuxhelp ~]#

Step 6: Again, execute the script by using the following command.

[root@linuxhelp ~]# ./casestatement
Are you a Student  [Yes] or [No] : n
No,I am a student[root@linuxhelp ~]#

Step 7: Again, execute the script by using the following command

[root@linuxhelp ~]# ./casestatement
Are you a Student  [Yes] or [No] : t
Invalid Response[root@linuxhelp ~]#

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to use case Statement on Bash Script. Your feedback is much welcome.

FAQ
Q
Mention the disadvantages of bash scripts
A
• Some disadvantages of bash script are mentioned below:
• It works slower than other languages.
• The improper script can damage the entire process and generate a complicated error.
Q
What are the advantages of using bash scripts?
A
• It is easy to use and learn.
• Many manual tasks that need to run frequently can be done automatically by writing a bash script.
• The sequence of multiple shell commands can be executed by a single command.
Q
How to run multiple bash script in parallel?
A
Multiple bash scripts can be executed in parallel by using nohup command. How multiple bash files can be executed in parallel from a folder is shown in the following example.
Q
What is the alternative command for echo?
A
The alternative command for echo is tput. This command allows us to control how the output is displayed on the screen.
Q
Give the purpose of the shebang line.
A
The shebang line is at the top of each script used to determine the engine's location and is used to execute the script.