由於空格導致的通配錯誤

由於空格導致的通配錯誤

我的目錄變數

POSTMAP="/work/Documents/Projects/untitled\ folder/untitled\ folder/*/*_tsta.bam"

我的 for 聲明:

for file0 in ${POSTMAP}; do
...

看來「無標題資料夾」中的空格與通配符混淆了。我懷疑這是因為 file0 最終成為“/untitled”。請注意,我有“shopt -s extglob”。

答案1

這不是真的搞亂通配符。在這裡,透過使用$POSTMAP不含引號的,您正在使用 split+glob 運算子。

使用預設值$IFS, 在您的 上/work/Documents/Projects/untitled\ folder/untitled\ folder/*/*_tsta.bam,它會先將其拆分為"/work/Documents/Projects/untitled\","folder/untitled\""folder/*/*_tsta.bam"。只有第三個包含通配符,因此受 glob 部分的約束。但是,glob 只會搜尋folder相對於目前目錄的目錄中的檔案。

如果您只需要該運算子的glob一部分而不是其,請設定為空字串。對於該運算符,反斜線不能用於轉義分隔符(但僅在類似 Bourne 的 shell 中),它可以用於轉義通配符全域運算符。splitsplit+glob$IFS$IFSbashbash

所以要嘛:

POSTMAP="/work/Documents/Projects/untitled folder/untitled folder/*/*_tsta.bam"
IFS=   # don't split
set +f # do glob
for file0 in $POSTMAP # invoke the split+glob operator
do...

或者使用支援 、 、 、 等數組的 shellbash可能yashzsh更好ksh

postmap=(
  '/work/Documents/Projects/untitled folder/untitled folder/'*/*_tsta.bam
) # expand the glob at the time of that array assignment
for file0 in "${postmap[@]}" # loop over the array elements
do....

相關內容