Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


CompTIA Linux+ XK0-005 - 3.1 - Shell Script Elements: case/switch Statement

Switch/case statements are control flow structures in shell scripting that allow you to compare a variable or an expression against multiple values or patterns and execute different blocks of code based on the match. This construct provides a concise and readable way to handle multiple conditions in your scripts.

Basic Syntax

The basic syntax of a switch/case statement in shell scripting is as follows:

case expression in
    pattern1)
        # Code to be executed if expression matches pattern1
        ;;
    pattern2)
        # Code to be executed if expression matches pattern2
        ;;
    pattern3)
        # Code to be executed if expression matches pattern3
        ;;
    *)
        # Code to be executed if none of the patterns match
        ;;
esac
  • expression is the value or variable to be matched against the patterns.
  • pattern1, pattern2, etc., are values or patterns that are compared with the expression.
  • The code block following each pattern is executed if the expression matches the pattern.
  • Each code block ends with ;; to indicate the end of the case.
  • The *) pattern serves as the default case if none of the patterns match.
  • The statement ends with esac.

Example: Matching Days of the Week

#!/bin/bash

day=$(date +%A)

case $day in
    "Monday")
        echo "It's the first day of the week."
        ;;
    "Tuesday"|"Wednesday"|"Thursday")
        echo "It's a weekday."
        ;;
    "Friday")
        echo "It's Friday!"
        ;;
    "Saturday"|"Sunday")
        echo "It's the weekend."
        ;;
    *)
        echo "Invalid day."
        ;;
esac

In this example, the switch/case statement retrieves the current day using the date command with the %A format specifier. The value is stored in the day variable. Depending on the value, different code blocks are executed. If the value is "Monday", it prints "It's the first day of the week." If the value is "Tuesday", "Wednesday", or "Thursday", it prints "It's a weekday." If the value is "Friday", it prints "It's Friday!" If the value is "Saturday" or "Sunday", it prints "It's the weekend." If none of the patterns match, the script executes the default case and prints "Invalid day."

Conclusion

Switch/case statements provide an efficient and structured way to handle multiple conditions in shell scripting. By utilizing different patterns and code blocks, you can easily control the flow of your script based on various values or expressions.

Remember to customize the patterns and code blocks within the switch/case statement to suit your specific requirements. You can have as many patterns as necessary to cover all possible cases. Ensure that each code block ends with ;; to indicate the end of a case.


Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.