0

From Bash Manual, for filename expansion:

If the nullglob option is set, and no matches are found, the word is removed.

My following script will return exit status 1.

  • Can filename expansion failing to match any jpg file in my current directory cause my script to exit with 1?

  • How can I rule out other possibility for exit status 1?

Thanks.

shopt -s nullglob

for i in *.{png,jpg};
do
    filename=${i##*/}
    basename=${filename%.*}
    [ ! -e ${basename}.pdf ] && convert  "$i"  ${basename}.pdf ;
done
  • Run the script with bash -x to see where it exits exactly, and why. – Stephen Kitt Sep 26 '17 at 11:30
  • Does not exit with status 1 here, when running in a directory with no files matching the pattern. – Kusalananda Sep 26 '17 at 11:34
  • @Kusalananda thanks. without shopt -s nullglob, will no matching cause your script exit with 1? – Tim Sep 26 '17 at 11:35
  • @Tim Yes, since the commands in the loop fail. – Kusalananda Sep 26 '17 at 11:36
  • @Stephen: Thanks. I updated my script, because I missed the checking the pdf file existence in my oriignal post. – Tim Sep 26 '17 at 11:39
1

No, setting the nullglob shell option and failing to expand a glob pattern will not cause the script to exit with a non-zero exit status (and matching filenames with glob patterns in general does not change the $? shell variable).

Not setting nullglob will make convert fail (unless there are files with the literal names *.jpg and *.png in the current directory), and it will exit with an exit status of 1.

Since this is the last executed command in the script, the script will exit with this exit status.


After update to script in question:

The script now exits with a non-zero exit status if a PDF file exists for the last image file processed, due to the [ ! -e ... ] test (this is the last command executed in the script in that case).

If this is unwanted, use

[ ! -e "${basename}.pdf" ] && convert "$i" "${basename}.pdf" || true

(note also the added quoting)

  • Thanks. I updated my script, because I missed the checking the pdf file existence in my oriignal post. Can that be the reason for script exist status 1? How can I suppress that cause of exit status 1? – Tim Sep 26 '17 at 11:39
  • @Tim See update. – Kusalananda Sep 26 '17 at 11:53

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.