我无法理解以下代码在 bash 完成脚本中的作用:
case "$last" in
+\(--import|-i\))
_filedir '+(txt|html)';;
什么时候遇到过这种情况?我认为上面的第二行会是这样的
--import|-i)
这对我来说确实有意义。我在 bash_completion.d
目录中查找了 '+\\('
,但这是唯一出现的一个,所以我猜这并不常见。
最佳答案
这段代码在没有上下文的情况下确实令人费解。事实上,它匹配两个文字字符串 -
$ case "+(--import" in +\(--import|-i\)) echo match ;; esac
match
$ case "-i)" in +\(--import|-i\)) echo match ;; esac
match
它看起来类似于扩展的 glob 模式 +(--import|-i)
,但在这种形式中,它既不匹配文字模式(需要转义管道),也不匹配实际模式(需要取消括号)。我猜是“bug”,但 bash 补全是疯狂元编程的雷区,所以在没有看到整个脚本的情况下不可能说出来。
来自bash(1)
If the extglob shell option is enabled using the shopt builtin, several extended pattern matching operators are recognized. In the following description, a pattern-list is a list of one or more patterns separated by a |. Composite patterns may be formed using one or more of the following sub-patterns:
[...]
+(pattern-list) Matches one or more occurrences of the given patterns
关于bash - bash 完成脚本的令人困惑的情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12489020/