2016-01-13 39 views
0

我需要一個腳本,該腳本可以在給定路徑中查找具有給定擴展名的文件,但不包括目錄,它們的名稱取自文件。如何獲得查找命令文件中的「!-path」參數

我們有一個script.sh和一個white.list文件。 white.list包含:

/path/something

/path/someotherthing

腳本:

whitelistfile=/scriptdir/white.list 
whitelist=`cat $whitelistfile` 

if test -n "$whitelist" 
then 
    # collect whitelist paths 
    for listitem in $whitelist; 
    do 
     excludepath+="! -path \"${listitem}*\" " 
    done 

files=`find "$path" $excludepath -name *."$3" -type f` 

fi 

echo $files 

的問題是,發現不接受$ EXCLUDEPATH說法。說沒有這樣的目錄。

有人可以幫我解決這個問題嗎?

回答

0

您會使用一個數組,如我在your other question中所解釋的。 Bash FAQ有一個關於從文件中讀取行的內容。

這樣:

whitelistfile=/scriptdir/white.list 

while IFS= read -r; do 
    excludepath+=(! -path "$REPLY") 
done <$whitelistfile 
[[ $REPLY ]] && excludepath+=(! -path "$REPLY") 

files=`find "$path" "${excludepath[@]}" -name "*.$3" -type f`