2014-04-09 36 views
0

當我試圖阻止通配符擴展到下面的腳本時,它不起作用。例如,在外殼I型:(我也曾嘗試\ * M 「* .M」。)禁用通配符擴展到Bash腳本不能正常工作

$ getLargeNumFiles.sh $(pwd) '*.m' 

我也得到

$ myDirectory 
$ file1.m file2.m file3.m 
$ find: paths must precede expression 
$ Usage: find [-H] [-L] [-P] [path...] [expression] 
$ 0 #The result of my function. Found 0 .m files. Should be 3. 

getLargeNumFiles.sh

#!/bin/bash 
# Syntax 
# getLargeNumFiles [folderPath][ext] 
# [folderPath] The path of the folder to read. 
# To use at folder to search: $(pwd) 

function largeRead() 
{ 
    numFiles=0 
    while read line1; do 
     #echo $line1 
     ((numFiles++)) 
    done 
    echo $numFiles 
} 
folderPath=$1 
ext=$2 

echo $folderPath 
echo $ext 

find $folderPath -name $ext | largeRead 

回答

2

將變量置於引號中,以防擴展變量後的通配符擴展:

find "$folderPath" -name "$ext" | largeRead 
+0

謝謝!它效果很好! – ABC