bash 文件通配异常
bash file globbing anomaly
bash 手册(我在 OSX 上使用 4.3.42 版)指出竖线“|”字符在文件通配中用作多个文件模式的分隔符。因此,以下内容应该适用于我的系统:
projectFiles=./config/**/*|./support/**/*
但是,第二个模式在该目录结构中的最后一个文件上给出了 "Permission denied",因此该模式永远不会解析为 projectFiles。我已经尝试过对此的变体,包括将模式包装在括号中,
projectFiles=(./config/**/*)|(./support/**/*)
手册中有规定,但也没用。
对我做错了什么有什么建议吗?
您可能指的是 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 在评论中指出的那样:
Note that you can do this without extglob
, ./{config,support}/**/*
, which would just expand to the path with config and the path with support space delimited and then do pattern matching. Or ./@(config|support)/**/*
with extglob
. Either of which seems cleaner.
@chepner的评论也值得一提:
Also, globbing isn't performed at all during a simple assignment; try foo=*
, then compare echo "$foo"
with echo $foo
. Globbing does occur during array assignment; see foo=(*); echo "${foo[@]}"
bash 手册(我在 OSX 上使用 4.3.42 版)指出竖线“|”字符在文件通配中用作多个文件模式的分隔符。因此,以下内容应该适用于我的系统:
projectFiles=./config/**/*|./support/**/*
但是,第二个模式在该目录结构中的最后一个文件上给出了 "Permission denied",因此该模式永远不会解析为 projectFiles。我已经尝试过对此的变体,包括将模式包装在括号中,
projectFiles=(./config/**/*)|(./support/**/*)
手册中有规定,但也没用。
对我做错了什么有什么建议吗?
您可能指的是 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 在评论中指出的那样:
Note that you can do this without
extglob
,./{config,support}/**/*
, which would just expand to the path with config and the path with support space delimited and then do pattern matching. Or./@(config|support)/**/*
withextglob
. Either of which seems cleaner.
@chepner的评论也值得一提:
Also, globbing isn't performed at all during a simple assignment; try
foo=*
, then compareecho "$foo"
withecho $foo
. Globbing does occur during array assignment; seefoo=(*); echo "${foo[@]}"