当我试图阻止通配符扩展到以下脚本时,它不起作用。例如,在shell中,我键入:(我还尝试过\*.m,“*.m”)
$ 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
最佳答案
将变量放在引号中,以防止变量展开后通配符展开:
find "$folderPath" -name "$ext" | largeRead