用于解压缩文件的 Bash 脚本——得到错误:无法找到或打开

som*_*me1 0 bash zip

这是我的脚本:

Z=/var/pixel/sftp/gisftp/pixel_images/pixel_images_archive.zip
if [ $Z ]; then
    echo "$Z exists"
    unzip $Z -d test
else
   echo "$Z doesn't exist!"
fi
Run Code Online (Sandbox Code Playgroud)

运行脚本时我得到了这个:

/var/pixel/sftp/gisftp/pixel_images/pixel_images_archive.zip exists
unzip:  cannot find or open
/var/pixel/sftp/gisftp/pixel_images/pixel_images_archive.zip, /var/pixel/sftp/gisftp/pixel_images/pixel_images_archive.zip.zip or /var/pixel/sftp/gisftp/pixel_images/pixel_images_archive.zip.ZIP.
Run Code Online (Sandbox Code Playgroud)

怎么存在,又不存在??

我应该如何解决这个问题?

Ale*_*exP 6

[ $Z ]只测试字符串不为空(并且[ -n "$Z" ]本来是可取的),而不是字符串命名的文件存在。[ -e "$Z" ]为此使用。请参阅test您的 shell的手册页或文档(因为[可能是内置的 shell)。

test(它是 的全名[)还允许您检查文件是否存在且不为空 ( test -s "$Z"),或者它是否存在且是常规文件 ( test -f "$Z") 等。