So I have a bash script as follows:
Command1 -- If error occurs, ignore error and continue execution
Command2 -- If error occurs, ignore error and continue execution
Command3 -- If error occurs, stop execution, exit the script with the error
Command4 -- If error occurs, stop execution, exit the script with the error
Command5 -- If error occurs, stop execution, exit the script with the error
Command6 -- If error occurs, ignore error and continue execution
I need to check specific commands to see if they threw an error and if they did, exit the script with an error code (Comand3,4,5). However, I cannot use "set -e" because I wish to purposefully ignore certain commands as well(Command1,2,6).
Is there any way I can combine the commands 3, 4 and 5 within a function and run it such that the script exits if any of these commands throw an error? Also, if I wish to do some cleanup before the script exits (similar to catch clause in Java, where the code executes if there is an exception), how can I do so?
Thanks
How about:
Command1
Command2
Command3 || exit
Command4 || exit
Command5 || exit
Command6
or
Command1
Command2
Command3 && Command4 && Command5 || exit
Command6
To do your cleanup:
cleanup() {
# commands here that do your cleanup
# ...
}
Command3 && Command4 && Command5 || { cleanup; exit; }