我在这里注意到我的脚本存在问题。这里的大问题来自我的fprintf没有正确地将换行保存到文件。例如,当我使用特定的测试文件时,应该保存47个新行。我已经尝试了自己的find命令并将其传送到wc -l以获取该号码。但是,当我尝试保存到一个文件(为每个结果保存一个新行并尝试计算它)时,我只得到35.对我来说更陌生的是,当我交换文件的顺序时扩展(例如首先搜索gif)它也会保存新的行(执行gif首先保存3行)。所以我不确定这笔交易是什么。
我遇到的另一个问题是打印出正确的文件大小。例如,在我使用的一个测试文件中,我的文件总大小为2,908,160,应该是2,628,419。
这是我的剧本:
#!/bin/bash
#Godfried Weihs
#Lab 2 - Search and Report
#CS 3030 - Scripting
#If Statement checks for usage, prints appropriate message if path is empty
#+ else it runs through the scripts normally
if [ -z "$1" ]; then
echo Usage: srpt path
exit 1
else
echo -e "SearchReport $HOSTNAME $(basename "$1") $(date)\n"
#Finds all required files and saves them to temporary files to be used for count output
find $1 -mindepth 1 \
\( -type d -fprintf /tmp/dcnt "\n" \) , \
\( -type f -fprintf /tmp/fcnt "\n" \) , \
\( -type l -fprintf /tmp/scnt "\n" \) , \
\( -type f -mtime +365 -fprintf /tmp/ocnt "\n" \) , \
\( -type f -size +500000c -fprintf /tmp/lcnt "\n" \) , \
\( -type f -name *.jpg -o -name *.bmp -o -name *.gif -fprintf /tmp/gcnt "\n" \) , \
\( -type f -name '*.o' -fprintf /tmp/tcnt "\n" \) , \
\( -type f -executable -fprintf /tmp/ecnt "\n" \) , \
\( -type f -fprintf /tmp/total "$(du -ch -B1 $1)" \)
#Print out results saved in tmp folder
echo Execution time $SECONDS
printf "Directories %'d\n" $(cat /tmp/dcnt | wc -l)
printf "Files %'d\n" $(cat /tmp/fcnt | wc -l)
printf "Sym links %'d\n" $(cat /tmp/scnt | wc -l)
printf "Old files %'d\n" $(cat /tmp/ocnt | wc -l)
printf "Large files %'d\n" $(cat /tmp/lcnt | wc -l)
printf "Graphics files %'d\n" $(cat /tmp/gcnt | wc -l)
printf "Temporary files %'d\n" $(cat /tmp/tcnt | wc -l)
printf "Executable files %'d\n" $(cat /tmp/ecnt | wc -l)
#printf "Total file size %'d\n" $(du -hs -B1 $1 | cut f -1)
#printf "Total file size %'d\n" $(du -ch -B1 $1 | tail -n1 | cut -f 1)
printf "Total file size %'d\n" $(cat /tmp/total | tail -n1 | cut -f 1)
fi
我觉得我还有一些我没看到的小事。任何帮助将不胜感激。
答案 0 :(得分:2)
引用带有图像文件扩展名的参数,否则通配符将由shell扩展而不是传递给SETX
(除非当前目录中没有匹配的文件)。此外,您需要将它们与括号一起分组,因为SETX /?
的优先级低于组合相邻表达式的隐含find
;这就是扩展顺序重要的原因,你只为最后一个表达式运行-o
。
-a
关于文件总大小,-fprintf
获取磁盘块中每个文件的大小,而不是文件长度(以字节为单位)。因此,报告的总数将大于所有文件的总长度,因为它们全部舍入到下一个块大小。