What Is Case in Shell Scripting?

//

Scott Campbell

In shell scripting, a case statement is used to perform different actions based on the value of a variable or an expression. It provides a more efficient and readable way to handle multiple conditions compared to using multiple if-else statements.

Basic Syntax

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

case variable in
    pattern1)
        # code block for pattern1
        ;;
    pattern2)
        # code block for pattern2
        ;;
    pattern3)
        # code block for pattern3
        ;;
    *)
        # default code block
        ;;
esac

The case statement starts with the case keyword followed by the variable or expression that needs to be evaluated. It then uses the in keyword to specify the possible patterns or values that will be matched.

The patterns are enclosed in parentheses and followed by a code block that will be executed if the pattern matches the value of the variable or expression. Each code block ends with double semicolons (;;).

The case statement also allows for a default code block denoted by an asterisk (*). This code block is executed if none of the specified patterns match the value of the variable or expression.

Example Usage

To further illustrate how a case statement works, let’s consider an example:

# Shell script example - file_type.sh
#!/bin/bash

echo "Enter a file name: "
read filename

case $filename in
    *.txt)
        echo "Text file"
        ;;
    *.pdf)
        echo "PDF file"
        ;;
    *.jpg|*.png)
        echo "Image file"
        ;;
    *)
        echo "Unknown file type"
        ;;
esac

In this example, the user is prompted to enter a file name. The case statement then checks the value of $filename against different patterns to determine the type of the file.

If the file name ends with .txt, it matches the pattern *.txt and the code block inside that pattern is executed, displaying “Text file”. Similarly, if it ends with .pdf, it matches the pattern *.pdf and displays “PDF file”.

If the file name ends with either .jpg or .png, it matches the pattern *.png and displays “Image file”. If none of these patterns match, the default code block is executed and “Unknown file type” is displayed.

Nested Case Statements

In more complex scenarios, you may need to use nested case statements to handle multiple levels of conditions. This involves placing a case statement within another case statement.

# Shell script example - grade.sh
#!/bin/bash

echo "Enter your score: "
read score

case $score in
    90*)
        echo "Your grade is A"
        ;;
    80*)
        echo "Your grade is B"
        ;;
    70*)
        echo "Your grade is C"
        ;;
    *)
        case $score in
            [0-6]*)
                echo "Your grade is F"
                ;;
            *)
                echo "Invalid score"
                ;;
        esac
        ;;
esac

In this example, the user enters a score, and the outer case statement evaluates the value of $score to determine the grade. If the score starts with 90, it matches the pattern 90* and displays “Your grade is A”. Similarly, for scores starting with 80 or 70, it displays “B” or “C” respectively.

If none of these patterns match, the inner case statement is executed. It checks if the score starts with a digit from 0 to 6 and displays “Your grade is F”. For any other score, it displays “Invalid score”.

Conclusion

The case statement in shell scripting provides a powerful tool for handling multiple conditions efficiently. By using patterns and code blocks, you can easily execute different actions based on the value of a variable or expression. Nested case statements further enhance its flexibility in handling complex scenarios.

Remember to use proper indentation and comment your code to improve readability and maintainability. Experiment with different patterns and actions to fully utilize the capabilities of the case statement in your shell scripts.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy