bash - bash 完成脚本的令人困惑的情况

标签 bash

我无法理解以下代码在 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/

相关文章:

linux - 在 Bash 中显示长时间运行的应用程序的进度

bash - bash 命令结束时发出声音

bash - 使用 awk/bash 过滤 ldapsearch

linux - 脚本中的别名

bash - 服务器重启后windows批处理文件启动浏览器

linux - 在 shell 脚本中找不到命令

linux - 我需要 "cat"不同位置的一堆文件

linux - 如果 Linux 进程失败或崩溃,自动重新启动它

linux - 如何从指定文件中替换IP地址

c++ - 通过 bash 脚本进行程序交互和测试