2011-08-01 54 views
4

當進入目錄進入命令行模式,這一點:問題清單文件在bash有空格的目錄路徑

ls -d -1 "/Volumes/Development/My Project/Project"/**/* | grep \.png$ 

打印在.png結束所有文件的列表。

然而,當我試圖創建一個腳本:

#! /bin/bash 

clear ; 

# Tempoary dir for processing 
mkdir /tmp/ScriptOutput/ ; 

wdir="/Volumes/Development/My Project/Project" ; 

echo "Working Dir: $wdir" ; 

# Get every .PNG file in the project 
for image in `ls -d -1 "$wdir"/**/* | grep \.png$`; do 
...  
done 

我得到的錯誤:

cp: /Volumes/Development/My: No such file or directory 

空間導致一個問題,但我不知道爲什麼?

回答

0

如果你細使用while read和子由管道創建的,您可以:

find . -name '*.png' | while read FILE 
do 
    echo "the File is [$FILE]" 
done 
+0

你甚至可以找到。 -name'* .png'-exec echo {} \;''。如果需要,可以多次使用-exec選項查找。 – jfg956

+0

右鍵 - 對於一個命令(如echo),最好使用-exec(或| xargs以獲得更好的性能,如果有多個文件)。對於多個命令,我更喜歡while循環。爲我閱讀更容易。乾杯。 –

4

另一種選擇是改變IFS:

OLDIFS="$IFS" # save it 
IFS="" # don't split on any white space 
for file in `ls -R . | grep png` 
do 
    echo "$file" 
done 
IFS=$OLDIFS # restore IFS 

瞭解更多關於IFS在man bash

0

你可以試試,[[:空間:]代替空間

wdir="/Volumes/Development/My[[:space:]]Project/Project" 

或執行命令轉換單一的空間

wdir=`echo "$wdir" | sed 's/[[:space:]]/\[[:space:]]/g'`