Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
The Case Statement
2.1.
Explanation
2.2.
Example 1
2.3.
Example 2
2.4.
Example 3
3.
Frequently Asked Questions
3.1.
What is a shell script file?
3.2.
Why is shell scripting used?
3.3.
Is shell scripting easy?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Shell Scripting - The Case Statement

Author Sagar Mishra
1 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Hey Ninjas, you must have heard about the if/else or elif statements as a programmer. The if/else statement permits you to create a chain of if statements which is not always the best way, mainly when the values of every branch depend on the same variable. Shell has a Case statement that handles exactly the same problem.

Shell Scripting - The Case Statement

In this blog, we will discuss the Case Statement in Shell.

The Case Statement

The Case Statement

Case statements test input values until they match a pattern, executing the command linked with that input value. This makes it an excellent choice for menus where users can select an item to perform a matching action. 

The syntax of the Case statement is given below.

case $variableName in
pattern-1)
Statement(s);;
pattern-2)
Statement(s);;
pattern-3)
Statement(s);;
pattern-N)
Statement(s);;
*)
Statement(s);;
esac

Explanation

First, let's understand the meaning of syntax properly.

🧊 The case statement starts with the keyword "Case." After this, we have to give the variable name followed by the  "in" keyword.

🧊 We can use more than one pattern case separated by the | operator.

🧊 The ')' operator is used to ending the cases list.

🧊 There must be two semicolons (;;) after every pattern case body.

🧊 We can use special characters in pattern cases.

🧊 The return status is zero if there is no pattern match. Otherwise, it will return the matched pattern as a result.

🧊 The asterisk (*) symbol shows the default case, usually in the final pattern.

🧊 The esac keyword is used at the end of the syntax, which is used to end the program.

Example 1

After understanding the syntax properly, it's time to check the example for better clearance of the concept.

#!/bin/bash
echo "Which one is your favourite fruit?"
echo "1 - Apple"
echo "2 - Mango"
echo "3 - Banana"
echo "4 - Papaya"
echo "5 - Orange"
read fruit;
case $fruit in
	1) echo "Apple is my favourite fruit.";;
	2) echo "Mango is my favourite fruit.";;
	3) echo "Banana is my favourite fruit.";;
	4) echo "Papaya is my favourite fruit.";;
	5) echo "Orange is my favourite fruit.";;
	*) echo "This fruit is not available. Please select a different one.";;
esac

 

Output

Output

Example 2

This time we will check a different example. Let's see an example with multiple patterns.

#!/bin/bash
echo "Enter planet name: "
  read PLANET
  case $PLANET in
  	Mercury | Venus | Earth | Mars | Jupiter | Saturn | Uranus | Neptune)
  		echo "$PLANET is a planet from the Solar System.";;
  	Pluto)
  		echo "$PLANET is a dwarf-planet";;
  	"Planet Nine") echo "$PLANET not discovered yet";;
  	*)
  		echo "$PLANET is not from the Solar System.";;
  esac

 

Output:

Output

Example 3

We will now see the for loop in shell scripting. Let's start with our code.

#!/bin/bash
for ((i=1; i<5; i++))
do
    echo $i
done

 

Output:

Output

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

What is a shell script file?

A file containing one or more commands is known as a shell script. Shell scripts offer a simple way to execute hard tasks, lengthy or complex sequences of commands, and repetitive operations. The system runs the command sequence in a shell script file when its name is entered.

Why is shell scripting used?

Admins use them to get regular backups. Compared to other languages like C or C++, they are simpler to write and debug. The shell script can be copied to other UNIX and related operating systems and run there. Shell scripts are also employed for routine system monitoring.

Is shell scripting easy?

Shell script syntax is similar to other languages. It would be quite simple to start with it if you have prior knowledge of any programming language, such as Python, C/C++, etc.

Conclusion

This article discusses the topic of the Case Statement in Shell Scripting. In detail, we have seen the Case Statement in Shell Scripting with a proper explanation. We have also seen the example of the Case Statement with output.

We hope this blog has helped you enhance your knowledge of the Case Statement in Shell Scripting. If you want to learn more, then check out our articles.

⚡ Shell Scripting - Array variables.

⚡ Shell Scripting - Read User Input.

⚡ Shell Scripting  - While Loop.

And many more on our platform Coding Ninjas Studio.

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations.

However, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Previous article
Shell scripting - If Statement
Next article
Shell Scripting - Read User Input
Live masterclass