bash - Capturing exit status of a failed command in a shell script -
i have shell script multiple commands below
cmd-1 cmd-2 cmd-3 .... cmd-n
i want shell script execution continue if there failure in middle e.g cmd-3 or cmd-7. achieve used set +e. allows me continue execution, unable capture exit status of failed command (since exit status of script based on last command). there way set status of complete script based on last failed command.
you can achieve that. not clean way, quite easy.
exit_code=0 cmd-1 || exit_code=$? cmd-2 || exit_code=$? cmd-3 || exit_code=$? .... cmd-n || exit_code=$? exit $exit_code
the clean way split code functions , check result of commands there.
Comments
Post a Comment