The Ultimate Guide to Using Case Statements in Shell Scripts

The Ultimate Guide to Using Case Statements in Shell Scripts
Shell scripts are a powerful tool for automating tasks and simplifying complex workflows. 

One key feature of shell scripts is the ability to make decisions based on the value of a variable or expression.

In this article, we will explore the case statement in shell script, a construct that allows you to write efficient, easy-to-read code that handles multiple scenarios.

What is a Case Statement in Shell Script?

A case statement is a decision-making construct in shell script that allows you to compare a variable or expression to a set of possible values. It is a more efficient and elegant alternative to using multiple if-else statements.

The syntax of a case statement in shell script is as follows:


case expression in
pattern1)
statements;;
pattern2)
statements;;
pattern3)
statements;;
*)
default statements;;
esac

The expression is the variable or expression that is being compared, and the patterns are the possible values that it can take.

The patterns are separated by vertical bars. The statements within each pattern are executed if the expression matches that pattern.

The default statements are executed if none of the patterns match.

Let's take a look at some examples of how to use the case statement in shell script.

Example 1: Checking the value of a variable

Suppose we have a variable called $fruit that contains the name of a fruit. We want to execute a different set of statements depending on the value of $fruit.

We can use the case statement to achieve this as follows:


#!/bin/bash

fruit="banana"

case $fruit in
"apple")
echo "This is an apple.";;
"banana")
echo "This is a banana.";;
"orange")
echo "This is an orange.";;
*)
echo "Unknown fruit.";;
esac

When we run this script, we get the following output:


This is a banana.

The statements within the banana pattern are executed because $fruit is equal to "banana".


Example 2: Checking the output of a command

Suppose we want to check the status of a service running on our machine using the systemctl status command.

We want to execute a different set of statements depending on whether the service is running or not.

We can use the case statement to achieve this as follows:


#!/bin/bash

status=$(systemctl status apache2)

case "$status" in
*"active (running)"*)
echo "Apache is running.";;
*"inactive (dead)"*)
echo "Apache is not running.";;
*)
echo "Unknown status.";;
esac

When we run this script, we get the following output:


When we run this script, we get the following output:

The statements within the active (running) pattern are executed because $status contains the string "active (running)".

Advantages of Using Case Statements

There are several advantages to using case statements in shell script, Some of them are:

Readability: Case statements make your code easier to read and understand because they allow you to handle multiple scenarios in a structured and organized way.

Efficiency: Case statements are more efficient than using multiple if-else statements because they only compare the expression to the patterns once.

Flexibility: Case statements allow you to handle multiple scenarios with ease, and they can be nested within other constructs like loops and functions.

In conclusion, the case statement in shell script is a powerful construct that allows you to write efficient, easy-to-read code that handles multiple scenarios. By using case statements in your shell scripts, you can improve the readability, efficiency, and flexibility of your code.