在case语句中的模式脚本的bash [英] patterns in case statement in bash scripting

查看:141
本文介绍了在case语句中的模式脚本的bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

页说,情况语句中使用文件名扩展模式匹配。结果
我通常要对某些参数短名称,所以我去:

The man page says that case statements use "filename expansion pattern matching".
I usually want to have short names for some parameters, so I go:

case $1 in
    req|reqs|requirements) TASK="Functional Requirements";;
    met|meet|meetings) TASK="Meetings with the client";;
esac

logTimeSpentIn "$TASK"

我试着像 REQ *模式我{E,}牛逼我的理解会正确地扩展,以便与在文件名扩展的上下文中的那些值,但它不工作

I tried patterns like req* or me{e,}t which I understand would expand correctly to match those values in the context of filename expansion, but it doesn't work.

推荐答案

括号扩展不起作用,但 * [] 做的。如果设置禁用了javascript -s extglob ,那么你也可以使用:

Brace expansion doesn't work, but *, ? and [] do. If you set shopt -s extglob then you can also use:


  • () - 零个或一个模式

  • *() - 零个或多个模式

  • +() - 模式的一次或多次出现

  • @() - 模式出现一次

  • ()! - 除了模式什么

  • ?() - zero or one occurrences of pattern
  • *() - zero or more occurrences of pattern
  • +() - one or more occurrences of pattern
  • @() - one occurrence of pattern
  • !() - anything except the pattern

下面是一个例子:

case $1 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
esac

这篇关于在case语句中的模式脚本的bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆