I am trying to do something similar to "try.. catch" with bash, for which I read from this post that a good option to somehow replicate try/catch is to use the || and && operators. In my case, I have a piece of code supposed to zip some files, but the zip is actually empty, so it throw an error:
zip -v OUTPUT.zip file1 file2 >> $LOGFILE 2>&1
Then in the LOGFILE, I see:
zip error: Nothing to do! (OUTPUT.zip)
zip warning: OUTPUT.zip not found or empty
...(continued error message)
So I do this instead, to "catch the error"
{ zip -v OUTPUT.zip file1 file2 >> $LOGFILE 2>&1 } || { printf "Error while zipping!" >> $LOGFILE && exit 1 }
..which partially works. It does exit the code, but doesn't do the printf command (or at least I can't see it in the LOGFILE).
Also, although it encounters the "exit 1" command (on line 115), I also have the message (line 120 is my last line):
line 120: syntax error: unexpected end of file
What is wrong in my command? Why doesn't it do the print, and why the "unexpected end of file" message? Sorry if this question is more related to general bash programming than to the use of || &&, but I didn't know how to categorize this otherwise.
Thanks a lot!