【发布时间】:2013-10-17 20:41:45
【问题描述】:
我正在做一个课程项目!作业文本如下:
编写一个以单词和数字为参数的 shell 脚本。 然后它检查当前目录中的所有文件,并找出哪些文件 至少包含给定的单词给定的次数。
样本输出应该是:
$myprog3.sh write 2
The file "./file-comp.sh" contains the word "write" 3 times.
The file "./homework.log" contains the word "write" 11 times.
我写了一些代码,但是在将文件名读入数组时遇到了问题。
count=`find . -type f -exec grep -H $word {} \; | wc -l`
read -a filearray <<< `find . -type f -exec grep -l "$word" {} \;`
read -a numarray <<< `find . -type f -exec grep -c "$word" {} \;`
size=${#filearray[@]}
echo "Array size is "$size""
for x in `seq 0 $size`
do
echo $x
echo "${filearray[x]}"
done
输出看起来像这样:
Array size is 5
0
./UntitledDocument.tex~
1
./Untitled
2
Document.tex
3
./wordcounter.sh
4
./wordcounter.sh~
5
例如:它应该看起来像 Untitled Document.tex 而不是
无标题
文档.tex
我该如何解决?
对于完整的问题,您能否为我提供一个解决方案? 提前谢谢..
【问题讨论】:
-
你的意思是 :size=${#filearray[@]} echo "Array size is "$size"" for x in "${filearray[@]}";do echo "$x " 完成 但还是一样 :(