我对 linux 比较缺乏经验, 如果某个 zip 文件为空,我必须检查 bash 脚本——即 zip 不包含任何文件。 我找到这段代码:
if ! zipinfo ${filetotransfer} | tail -n 1 | grep '^0 ' >/dev/null ; then # we have empty zip file!
echo " zip empty"
rm $filetotransfer
exit 0
fi
但是无论 zip 是否为空,它都会删除文件。 有什么方法可以检查吗?
最佳答案
您可以使用 stat 或 md5sum 或 zip header 检查文件大小是否为 22
# by checking file size
% stat -f%z a.zip
22
% xxd a.zip
0000000: 504b 0506 0000 0000 0000 0000 0000 0000 PK..............
0000010: 0000 0000 0000 ......
# with md5sum
$ md5sum a.zip
76cdb2bad9582d23c1f6f4d868218d6c a.zip
# or by checking zip header
% [ `head -n22 a.zip | tr -d '\0-\6'` = "PK" ] && echo 1
1
关于linux - Bash 脚本检查 zip 文件是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38814121/