• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

How to use Case Statement on Bash Script

  • 00:44 cat /etc/os-release
  • 01:00 vim casestatement
  • 05:14 chmod +x casestatement
  • 05:24 ll
  • 05:34 ./casestatement
  • 05:50 ./casestatement
  • 06:05 ./casestatement
7672

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.

Tags:
jacob
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

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.

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

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 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

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.

Related Tutorials in How to use Case Statement on Bash Script

Related Tutorials in How to use Case Statement on Bash Script

How to install Xrdp Server (Remote Desktop) on Oracle Linux 8.5
How to install Xrdp Server (Remote Desktop) on Oracle Linux 8.5
Oct 17, 2022
How to install and update OpenSSL on Debian 11.3
How to install and update OpenSSL on Debian 11.3
Oct 21, 2022
How to Install and Configure Mega in Linux
How to Install and Configure Mega in Linux
Jul 19, 2016
How to use Aureport command on Linux
How to use Aureport command on Linux
Nov 28, 2017
How to install Development tools on Linux
How to install Development tools on Linux
Jun 12, 2018
How to Install mod_ssl and SSL certificate on Oracle Linux
How to Install mod_ssl and SSL certificate on Oracle Linux
Dec 30, 2021
How to install Nextcloud on Ubuntu 22.04 version
How to install Nextcloud on Ubuntu 22.04 version
Jun 23, 2023
How to install ClipGrab in Linux
How to install ClipGrab in Linux
Jul 16, 2016

Related Forums in How to use Case Statement on Bash Script

Related Forums in How to use Case Statement on Bash Script

Linux
jayce class=
shasum command not found
May 5, 2017
Linux
stephan class=
How to list all samba users
Jan 12, 2018
pv command
muhammad class=
pvcreate command not found error
May 9, 2017
Linux
henry class=
Starting NFS daemon: rpc.nfsd: writing fd to kernel failed: errno 111 (Connection refused)
Apr 25, 2017
ifconfig command
jackbrookes class=
what is the location of the ifconfig program on your machine?
Jan 4, 2018
Linux
baseer class=
single command to apply setfacl for multiple user at a time
Jan 23, 2018
Linux
beulah class=
What does mean by 0 0 value in fstab file
Jan 2, 2018
CentOS
mason class=
Error getting authority: Error initializing authority: Could not connect: No such file or directory (g-io-error-quark, 1)
Nov 20, 2018

Related News in How to use Case Statement on Bash Script

Related News in How to use Case Statement on Bash Script

Anbox, the Android-to-Linux tool the developers have been waiting for
Anbox, the Android-to-Linux tool the developers have been waiting for
Apr 17, 2017
Linus Torvalds stops signing Linux kernel RC tarballs
Linus Torvalds stops signing Linux kernel RC tarballs
May 17, 2017
Capsule8 Launches Linux-Based Container Security Platform
Capsule8 Launches Linux-Based Container Security Platform
Feb 14, 2017
Symantec updates Management console product
Symantec updates Management console product
Nov 22, 2017
Latest Linux driver release feature seven AMD Vega
Latest Linux driver release feature seven AMD Vega
Mar 23, 2017
A Newer and a Faster Window Manager for Tina (Linux Mint 19.2)
A Newer and a Faster Window Manager for Tina (Linux Mint 19.2)
Apr 9, 2019
Microsoft makes its Azure App service now available on Linux Systems
Microsoft makes its Azure App service now available on Linux Systems
Sep 7, 2017
Docker friendly Alpine Linux gets hardened Node.js
Docker friendly Alpine Linux gets hardened Node.js
Apr 19, 2017
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help legeek ?
Installation of the call center module

hello

I wish to install a call center in virtual with issabel, I downloaded the latest version of it , but I don' t arrive to install the call center module in issabel. please help me

thanks!

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.