[英]bash file globbing anomaly
bash手冊(我在OSX上使用版本4.3.42)指出豎線“ |” 字符用作文件glob中多個文件模式的分隔符。 因此,以下應在我的系統上工作:
projectFiles=./config/**/*|./support/**/*
但是,第二個模式在該目錄結構中的最后一個文件上提供“權限被拒絕”,因此該模式永遠不會解析為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在評論中指出的那樣:
請注意,您可以在沒有
extglob
,././{config,support}/**/*
extglob
,./{config,support}/**/*
extglob
/./{config,support}/**/*
情況下執行此extglob
,./{config,support}/**/*
只會擴展到config路徑和支持空間定界的路徑,然后進行模式匹配。 或./@(config|support)/**/*
與extglob
。 兩者似乎都更干凈。
@chepner的評論也值得一提:
同樣,在簡單的分配過程中根本不執行globbing。 嘗試
foo=*
,然后將echo "$foo"
與echo $foo
。 數組分配過程中確實發生通配; 見foo=(*); echo "${foo[@]}"
foo=(*); echo "${foo[@]}"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.