I'm writing a bash script that should exit the error code of the last failed command and not continue execution. This can be achieved by adding a || exit $? everywhere, but is there an easier way, e.g. a set option at the start to do this without uglifying every line?
3 Answers
set -e ?
set: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
Set or unset values of shell options and positional parameters.
Change the value of shell attributes and positional parameters, or
display the names and values of shell variables.
Options:
-a Mark variables which are modified or created for export.
-b Notify of job termination immediately.
-e Exit immediately if a command exits with a non-zero status.
...
-
2Instead of the dummy padding you could quote the documentation.manatwork– manatwork2012-10-17 10:09:57 +00:00Commented Oct 17, 2012 at 10:09
-
-
1Note that non-zero return codes only cause a
set -escript to fail when the return codes aren't tested. The exact meaning of tested isn't easy to specify, but the basic idea is thatfailingcmd || nonfailingwon't make the whole script fail.dubiousjim– dubiousjim2012-10-17 18:25:55 +00:00Commented Oct 17, 2012 at 18:25 -
You might join all the commands with && and use || exit $? at the very end of the block.
For example:
#!/usr/bin/ksh
ls ~/folder &&
cp -Rp ~/folder ~/new_folder &&
rm ~/folder/file03.txt &&
echo "This will be skipped..." ||
exit $?
if there is no ~/folder/file03.txt file, the last echo command will be skipped. You should receive something like this:
$ ./script.ksh
file01.txt file02.txt
rm: cannot remove /export/home/kkorzeni/folder/file03.txt: No such file or directory
$ echo $?
1
Best regards, Krzysztof
-
1Hey thanks, and welcome to unix.stackexchange! As jordanm mentioned it turns out
exitalone will behave the same way asexit $?doesTobias Kienzler– Tobias Kienzler2012-10-18 05:35:17 +00:00Commented Oct 18, 2012 at 5:35
You can define a trap function to capture any error that is occurring in the script.
#!/usr/bin/ksh
trap errtrap
function errtrap {
es=$?
echo "`date` The script failed with exit status $es " | $log
}
rest of the script follows.
The TRAP will capture any error at any command and will call the errtrap function. For better usage you can make the errtrap function generic and call that in any script that you are creating.
-
Nice, but since you mention
ksh, does this also work inbash?Tobias Kienzler– Tobias Kienzler2016-02-05 09:35:17 +00:00Commented Feb 5, 2016 at 9:35 -
It works for bash as well.Pratik Mittal– Pratik Mittal2016-02-05 09:57:49 +00:00Commented Feb 5, 2016 at 9:57
-
I can't test this right now, but in that case +1 and welcome to unix.SE :)Tobias Kienzler– Tobias Kienzler2016-02-05 10:36:49 +00:00Commented Feb 5, 2016 at 10:36
exit $?is not needed. By default, your script will exit with the $? of the last command.exit $?andexitare equivalent in bash.$?was superfluous? Theexititself (withoutset -eas I learned) was necessary. But thanks, good to know for situations where I don't want to abort on all errors.$?is superfluous.