case 语句一般用于选择性来执行对应部分块命令。
格式:case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac

case 模式名	in
    模式 1)
        命令
        ;;
    模式 2)
        命令
        ;;
    *)
不符合以上模式执行的命令
esac

每个模式必须以右括号结束,命令结尾以双分号结束。

#!/bin/bash 
case $1 in
    start)
        echo "start."
        ;;
    stop)
        echo "stop."
        ;;
    restart)
        echo "restart."
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
esac


# bash test.sh
Usage: test.sh {start|stop|restart} 
# bash test.sh start
start.
# bash test.sh stop 
stop.
# bash test.sh restart 
restart.

上面例子是不是有点眼熟,在 Linux 下有一部分服务启动脚本都是这么写的。
模式也支持正则,匹配哪个模式就执行那个:

#!/bin/bash 
case $1 in
    [0-9])
        echo "match number."
        ;;
    [a-z])
        echo "match letter."
        ;;
    '-h'|'--help') 
        echo "help"
        ;;
    *)
        echo "Input error!" 
        exit
esac


# bash test.sh 1 
match number.
# bash test.sh a 
match letter.
# bash test.sh -h 
help
# bash test.sh --help 
help

模式支持的正则有:*、?、[ ]、[.-.]、|。后面有章节单独讲解 Shell 正则表达式。