Here is my script:

if [ $Z ]; then
    echo "$Z exists"
    unzip $Z -d test
   echo "$Z doesn't exist!"

I get back this when running the script:

/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.

How does it exist, but not exist??

How should I troubleshoot this?


[ $Z ] only tests that the string is not empty (and [ -n "$Z" ] would have been preferable), not that the file named by the string exists. Use [ -e "$Z" ] for that. See the manual page for test or the documentation of your shell (because [ may be a shell built-in).

test (which is the full name of [) also allows you to check whether a file exists and is not empty (test -s "$Z"), or if it exists and is a regular file (test -f "$Z") etc.

  • [ -e "$Z" ] worked, it turned out the path was wrong, thank you! – some1 Nov 21 at 0:10
  • 1
    Perhaps even -r as a test, since unzip will want to read it. – Jeff Schaller Nov 21 at 0:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.