I want to raise an error in a Bash script with message "Test cases Failed !!!". How to do this in Bash?
For example:
if [ condition ]; then
raise error "Test cases failed !!!"
fi
I want to raise an error in a Bash script with message "Test cases Failed !!!". How to do this in Bash?
For example:
if [ condition ]; then
raise error "Test cases failed !!!"
fi
This depends on where you want the error message be stored.
You can do the following:
Or the following:
When you raise an exception you stop the program's execution.
You can also use something like
exit xxx
wherexxx
is the error code you may want to return to the operating system (from 0 to 255). Here125
and64
are just random codes you can exit with. When you need to indicate to the OS that the program stopped abnormally (eg. an error occurred), you need to pass a non-zero exit code toexit
.As @chepner pointed out, you can do
exit 1
, which will mean an unspecified error.Here's a simple trap that prints the last argument of whatever failed to STDERR, reports the line it failed on, and exits the script with the line number as the exit code. Note these are not always great ideas, but this demonstrates some creative application you could build on.
I put that in a script with a loop to test it. I just check for a hit on some random numbers; you might use actual tests. If I need to bail, I call false (which triggers the trap) with the message I want to throw.
For elaborated functionality, have the trap call a processing function. You can always use a case statement on your arg ($_) if you need to do more cleanup, etc. Assign to a var for a little syntactic sugar -
Sample output:
Obviously, you could
Lots of room for design improvement.
The draw backs include the fact that
false
isn't pretty (thus the sugar), and other things tripping the trap might look a little stupid. Still, I like this method.Basic error handling
If your test case runner returns a non-zero code for failed tests, you can simply write:
Or even shorter:
Or the shortest:
To exit with test_handler's exit code:
Advanced error handling
If you want to take a more comprehensive approach, you can have an error handler:
then invoke it after running your test case:
or
The advantages of having an error handler like
exit_if_error
are:if
blocks that test exit codes for errorsRelated posts
__FILE__
,__LINE__
in BashThere are a couple more ways with which you can approach this problem. Assuming one of your requirement is to run a shell script/function containing a few shell commands and check if the script ran successfully and throw errors in case of failures.
The shell commands in generally rely on exit-codes returned to let the shell know if it was successful or failed due to some unexpected events.
So what you want to do falls upon these two categories
Depending on which one you want to do, there are shell options available to use. For the first case, the shell provides an option with
set -e
and for the second you could do atrap
onEXIT
Should I use
exit
in my script/function?Using
exit
generally enhances readability In certain routines, once you know the answer, you want to exit to the calling routine immediately. If the routine is defined in such a way that it doesn’t require any further cleanup once it detects an error, not exiting immediately means that you have to write more code.So in cases if you need to do clean-up actions on script to make the termination of the script clean, it is preferred to not to use
exit
.Should I use
set -e
for error on exit?set -e
was an attempt to add "automatic error detection" to the shell. Its goal was to cause the shell to abort any time an error occurred, but it comes with a lot of potential pitfalls for example,The commands that are part of an if test are immune. In the example, if you expect it to break on the
test
check on the non-existing directory, it wouldn't, it goes through to the else conditionCommands in a pipeline other than the last one, are immune. In the example below, because the most recently executed (rightmost) command's exit code is considered (
cat
) and it was successful. This could be avoided by setting by theset -o pipefail
option but its still a caveat.Recommended for use -
trap
on exitThe verdict is if you want to be able to handle an error instead of blindly exiting, instead of using
set -e
, use atrap
on theERR
pseudo signal.The
ERR
trap is not to run code when the shell itself exits with a non-zero error code, but when any command run by that shell that is not part of a condition (like in ifcmd
, orcmd ||
) exits with a non-zero exit status.The general practice is we define an trap handler to provide additional debug information on which line and what cause the exit. Remember the exit code of the last command that caused the
ERR
signal would still be available at this point.and we just use this handler as below on top of the script that is failing
Putting this together on a simple script that contained
false
on line 15, the information you would be getting asThe
trap
also provides options irrespective of the error to just run the cleanup on shell completion (e.g. your shell script exits), on signalEXIT
. You could also trap on multiple signals at the same time. The list of supported signals to trap on can be found on the trap.1p - Linux manual pageAnother thing to notice would be to understand that none of the provided methods work if you are dealing with sub-shells are involved in which case, you might need to add your own error handling.
On a sub-shell with
set -e
wouldn't work. Thefalse
is restricted to the sub-shell and never gets propagated to the parent shell. To do the error handling here, add your own logic to do(false) || false
The same happens with
trap
also. The logic below wouldn't work for the reasons mentioned above.You have 2 options: Redirect the output of the script to a file, Introduce a log file in the script and
Here you assume that the script outputs all necessary info, including warning and error messages. You can then redirect the output to a file of your choice.
The above command redirects both the standard output and the error output to your log file.
Using this approach you don't have to introduce a log file in the script, and so the logic is a tiny bit easier.
In your script add a log file either by hard coding it:
or passing it by a parameter:
It's a good idea to add the timestamp at the time of execution to the log file at the top of the script:
You can then redirect your error messages to the log file
This will append the error to the log file and continue execution. If you want to stop execution when critical errors occur, you can
exit
the script:Note that
exit 1
indicates that the program stop execution due to an unspecified error. You can customize this if you like.Using this approach you can customize your logs and have a different log file for each component of your script.
If you have a relatively small script or want to execute somebody else's script without modifying it the the first approach is more suitable.
If you always want the log file to be at the same location, this is the better option of the 2. Also if you have created a big script with multiple components then you may want to log each part differently and the second approach is your only option.
I often find it useful to write a function to handle error messages so the code is cleaner overall.
This takes the error code from the previous command and uses it as the default error code when exiting the whole script. It also notes the time, with microseconds where supported (GNU date's
%N
is nanoseconds, which we truncate to microseconds later).If the first option is zero or a positive integer, it becomes the exit code and we remove it from the list of options. We then report the message to standard error, with the name of the script, the word "ERROR", and the time (we use parameter expansion to truncate nanoseconds to microseconds, or for non-GNU times, to truncate e.g.
12:34:56.%N
to12:34:56
). A colon and space are added after the word ERROR, but only when there is a provided error message. Finally, we exit the script using the previously determined exit code, triggering any traps as normal.Some examples (assume the code lives in
script.sh
):