Linux Bash脚本检查zip文件是否为空

Linux Bash脚本检查zip文件是否为空,linux,bash,Linux,Bash,我对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是否为空,它都会删除文件。 有什么方法可以检查

我对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是否为空,它都会删除文件。
有什么方法可以检查它吗?

您可以检查
zipinfo-t

f=test.zip
if zipinfo -t "$f" > /dev/null
then
    echo "not empy"
else
    echo "empty"
fi

您可以检查
zipinfo-t的错误状态

f=test.zip
if zipinfo -t "$f" > /dev/null
then
    echo "not empy"
else
    echo "empty"
fi

您可以使用stat、md5sum或zip头检查文件大小是否为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

您可以使用stat、md5sum或zip头检查文件大小是否为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

您是否测试了您的条件是否按预期工作?
zipinfo${filetotransfer}tail-n1 | grep'^0'
的输出是什么?返回码是多少?我测试了它:它在所有情况下都会删除文件。事实上,这并不能回答我提出的两个具体问题中的任何一个。祝你好运您是否测试了您的条件是否按预期工作?
zipinfo${filetotransfer}tail-n1 | grep'^0'
的输出是什么?返回码是多少?我测试了它:它在所有情况下都会删除文件。事实上,这并不能回答我提出的两个具体问题中的任何一个。祝你好运