bash文件通配异常

时间:2016-05-28 17:35:40

标签: bash glob

bash手册(我在OSX上使用的是4.3.42版)声明垂直条'|' character用作文件globbing中多个文件模式的分隔符。因此,以下内容应该适用于我的系统:

projectFiles=./config/**/*|./support/**/*

但是,第二个模式在该目录结构中的最后一个文件上给出“权限被拒绝”,因此该模式永远不会被解析为projectFiles。我尝试过这方面的变化,包括将模式包装在括号中,

projectFiles=(./config/**/*)|(./support/**/*)

在手册中列出,但也不起作用。

有关我做错的任何建议吗?

1 个答案:

答案 0 :(得分:6)

你可能在man bash中指的是这部分:

   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 fol-
   lowing sub-patterns:

          ?(pattern-list)
                 Matches zero or one occurrence of the given patterns
          *(pattern-list)
                 Matches zero or more occurrences of the given patterns
          +(pattern-list)
                 Matches one or more occurrences of the given patterns
          @(pattern-list)
                 Matches one of the given patterns
          !(pattern-list)
                 Matches anything except one of the given patterns

|分隔符按照说明在模式列表中工作,但仅在启用extglob时才有效:

shopt -s extglob

试试这个:

projectFiles=*(./config/**/*|./support/**/*)

正如@BroSlow在评论中指出的那样:

  

请注意,您可以在没有extglob./{config,support}/**/*的情况下执行此操作,这只会扩展到包含配置的路径和支持空格分隔的路径,然后执行模式匹配。或./@(config|support)/**/*extglob。其中任何一个看起来更干净。

@chepner的评论也值得一提:

  

此外,在简单分配期间根本不执行globbing;尝试foo=*,然后将echo "$foo"echo $foo进行比较。在阵列分配期间确实发生了全球化;见foo=(*); echo "${foo[@]}"