Shell分支语句case···esac语法

Shell编程:case ... esac多分支选择编程

也多常用于菜单选择

语法:

case 值 in
模式1)
    command1
    command2
    command3
    ;;
模式2)
    command1
    command2
    command3
    ;;
*)
    command1
    command2
    command3
    ;;
esac

说明: case后为取值,值后为关键字 in,接下来是匹配的各种模式,每一模式最后必须以右括号结束。

             值可以为变量或常数。

             模式支持正则表达式,可以用以下字符:

*       任意字串
?       任意字元
[abc]   a, b, 或c三字元其中之一
[a-n]   从a到n的任一字元
|       多重选择

示例:

根据传递的第一个参数选择分支执行:

#!/bin/sh 

case $1 in
start | begin)
    echo "I am started!"  
    ;;
stop | end)
    echo "I am stopped!"  
    ;;
*)
    echo "Other command!"  
    ;;
esac

执行根据传递的第一个参数:start begin stop hello分别执行的结果

$./test.sh start
I am started!
$./test.sh stop
I am stopped!
$./test.sh begin
I am started!
$/test.sh hello
Other command!

详细使用方法,可参考:https://www.cnblogs.com/waitig/archive/2016/09/13/5868332.html

猜你喜欢

转载自blog.csdn.net/hpu11/article/details/80495219