我是 Bash 脚本的新手,我想制作一个脚本,该脚本将遍历目录以搜索子目录并进入其中以搜索可能位于前面提到的子目录中的文件中的特定字符串。
我目前遇到的问题实际上是第二部分。我正在尝试一一访问当前子目录中的所有文件,但它不起作用。这可能是一个愚蠢的问题,但由于我是新手,我发现自己无法找到解决问题的方法。这是代码:
if [[ $1 != "--help" ]];
then
echo "counting results for string: "$1>results.txt
let total=0
for dir in */
do
if [[ -d $dir ]];
then
echo "directory: "$dir>>../results.txt
let dirtotal=0
cd $dir
for i in */
do
if [[ -f $i ]];
then
#look in the file i and count the number of occurrences
let result=`grep -c $1 $i`
echo $i": "$result
let dirtotal=$dirtotal+$result
fi
done
echo "directory total: "$dirtotal>>../results.txt
let total=$total+$dirtotal
cd ..
fi
done
echo "total: "$total>>results.txt
exit 0
else
display_error
exit 1
fi
进行第二个 for...do...done 循环时会出现问题。
谢谢。