我想知道如何得到结果unzip -t file.zip以便我可以在 IF 语句中使用它
目前我有
if [ unzip -t "$file.zip" ]
then
#proceed with script
else
echo "Zip file is not ready"
fi
Run Code Online (Sandbox Code Playgroud)
我也试过,if [! unzip -t "$file.zip" ]但我想知道在继续脚本之前是否有办法确保文件有效。
我在一个相当简单的网络服务器上,我认为 gzip 没有运行,我无法使用 lsof 来确定文件是否仍在写入。
任何指向正确方向的点都会很棒。我搜索过谷歌和这些论坛,但没有运气。
谢谢。
代替:
if [ unzip -t "$file.zip" ]
Run Code Online (Sandbox Code Playgroud)
和:
if unzip -t "$file.zip"
Run Code Online (Sandbox Code Playgroud)
或者,如果您更喜欢安静:
if unzip -t "$file.zip" >/dev/null 2>&1
Run Code Online (Sandbox Code Playgroud)
以上应该适用于任何 POSIX shell。如果您bash以便携性为代价使用并更喜欢简洁,则上述内容可以简化为:
if unzip -t "$file.zip" &>/dev/null
Run Code Online (Sandbox Code Playgroud)
命令[,也称为test命令,用于根据某些条件设置退出代码,例如文件是否存在或字符串是否为空。这里不需要test命令。  unzip本身设置了if语句可以使用的适当退出代码。