我有一个脚本,用于检查大于1MB的压缩文件大小,并将文件及其大小作为报告输出。
这是代码:
myReport=`ls -ltrh "$somePath" | egrep '\.gz$' |  awk '{print $9,"=>",$5}'`
# Count files that exceed 1MB
oversizeFiles=`find "$somePath" -maxdepth 1  -size +1M -iname "*.gz" -print0 | xargs -0 ls -lh | wc -l`
 if [ $oversizeFiles -eq 0 ];then
    status="PASS"
 else
     status="CHECK FAILED. FOUND FILES GREATER THAN 1MB"
 fi
echo -e $status"\n"$myReport
问题是ls命令在报告中将文件大小输出为1.0MB,但状态为“失败”,因为变量“ $ oversizeFiles”的值为2。我检查了磁盘上的文件大小,其中2个文件为1.1MB。为什么会有这种差异?我应该如何修改脚本,以便生成准确的报告?
顺便说一句,我在Mac上。
这是我的Mac OSX上的“查找”手册页:
-size n[ckMGTP]
True if the file's size, rounded up, in 512-byte blocks is n.  
If n is followed by a c,then the primary is true if the file's size is n bytes (characters).  
Similarly if n is followed by a scale indicator then the file's size is compared to n scaled as:  
 k       kilobytes (1024 bytes)
 M       megabytes (1024 kilobytes)
 G       gigabytes (1024 megabytes)
 T       terabytes (1024 gigabytes)
 P       petabytes (1024 terabytes)