SoFunction
Updated on 2025-03-10

Implementation of branch control Case statement in Bash

When writing Bash scripts, we often need to execute different code blocks according to different conditions. Traditionalif-elif-elseStatements can become verbose and complex when dealing with multi-condition branches. Fortunately, Bash provides a more concise choice structure:caseStatement. In this article, we will explore in depthcaseHow to use statements and some practical examples.

What are Case statements?

caseA statement is a multi-branch selection structure that allows execution of different command sequences based on the value of a variable. andifCompared with statements,caseStatements are more intuitive and easy to read when dealing with pattern matching.

The syntax of the Case statement

caseThe basic syntax of a statement is as follows:

case $variable in
    pattern1)
        # If $variable matches pattern1, execute the command here        ;;
    pattern2)
        # If $variable matches pattern2, execute the command here        ;;
    *)
        # If $variable does not match any pattern, execute the command here        ;;
esac

The key points are as follows:

  • caseIt is followed by the variables that need to be matched.
  • Each pattern must end in the right parentheses.
  • The command sequence of the corresponding mode must be double semicolons;;Finish.
  • *)is a default pattern that is executed when no other pattern matches.
  • esaccaseThe reverse order ofcaseEnd of the statement.

Practical Examples

Let's illustrate with a practical examplecaseHow to use statements. Suppose we are writing a script to manage a service, and this script acceptsstartstoprestartandstatusAs a command line parameter.

#!/bin/bash

case "$1" in
    start)
        echo "Starting the service..."
        # Here you can place the command to start the service        ;;
    stop)
        echo "Stopping the service..."
        # Here you can place a command to stop the service        ;;
    restart)
        echo "Restarting the service..."
        # Here you can place the command to restart the service        ;;
    status)
        echo "Checking the status of the service..."
        # Here you can place a command to check the service status        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
        ;;
esac

In this script,$1Represents the first parameter of the command line.caseThe statement checks this parameter and executes the corresponding code block according to the value of the parameter.

Pattern matching flexibility

caseOne of the strengths of a statement is its flexibility in pattern matching. Patterns can contain wildcard characters, such as*Match any character of any length,?Match any single character,[abc]Match any character in square brackets.

For example, if we want to match any parameter that starts with "start", we can write this:

case "$1" in
    start*)
        echo "Starting something that begins with 'start'..."
        ;;
    ...
esac

Summarize

caseStatements are a very useful control structure in Bash scripts that provide scripts with a clear and concise way to handle multi-conditional branches. With pattern matching, it can easily deal with complex branching situations while keeping the code readable. Whether it is simple parameter analysis or complex conditional logic,caseStatements are powerful tools for Bash script writers.

Through the above introduction and examples, I hope you are now on how to use it in a Bash scriptcaseA deeper understanding of the sentence. Next time you need to write a conditional branch, try itcasestatements, it may make your code more elegant.

This is the article about the implementation of branch control Case statements in Bash. For more related contents of Bash Case statements, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!