加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在bash脚本中的case语句中使用模式?

发布时间:2020-12-15 19:04:30 所属栏目:安全 来源:网络整理
导读:手册页说case语句使用“文件名扩展模式匹配”. 我通常想要一些参数的短名称,所以我去: case $1 in req|reqs|requirements) TASK="Functional Requirements";; met|meet|meetings) TASK="Meetings with the client";;esaclogTimeSpentIn "$TASK" 我尝试过像r
手册页说case语句使用“文件名扩展模式匹配”.
我通常想要一些参数的短名称,所以我去:
case $1 in
    req|reqs|requirements) TASK="Functional Requirements";;
    met|meet|meetings) TASK="Meetings with the client";;
esac

logTimeSpentIn "$TASK"

我尝试过像req *或me {e,}这样的模式,我理解它会正确扩展以匹配文件名扩展上下文中的那些值,但它不起作用.

支撑扩展不起作用,但*,?和[]做.如果你设置shopt -s extglob然后你也可以使用 extended pattern matching:

>?() – 零或一次出现的模式
> *() – 出现零次或多次模式
>() – 一次或多次出现模式
> @() – 一次出现模式
>!() – 除了模式之外的任何东西

这是一个例子:

shopt -s extglob
for arg in apple be cd meet o mississippi
do
    # call functions based on arguments
    case "$arg" in
        a*             ) foo;;    # matches anything starting with "a"
        b?             ) bar;;    # matches any two-character string starting with "b"
        c[de]          ) baz;;    # matches "cd" or "ce"
        me?(e)t        ) qux;;    # matches "met" or "meet"
        @(a|e|i|o|u)   ) fuzz;;   # matches one vowel
        m+(iss)?(ippi) ) fizz;;   # matches "miss" or "mississippi" or others
        *              ) bazinga;; # catchall,matches anything not matched above
    esac
done

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读