I have a wrapper.sh script which call another script run_workflow.sh which eventually calls a workflow. I would like to handle error for run_wrklow.sh...i.e, if the workflow is executed successfully then i need to call another script run_workflow2.sh which triggers another workflow.
Here is the sample code...Please suggest me how to handle errors
wrapper.sh
sh run_workflow.sh #trigger workflow1
if [ $? -ne 0 ]; then
echo "Workflow Failed"
else
echo "Wrokflow Success"
sh run_workflow2.sh #trigger workflow2
if [ $? -ne 0 ]; then
echo "Workflow2 Failed"
else
echo "Workflow2 Success"
fi
fi
However when i try this code I'm not able to return failed status.
Here is my suggestion. You don't need to explicitly test $?, the syntax is that if is followed by a command ([ is the test command).
exit_value=1 # default failure
if sh run_workflow.sh #trigger workflow1
then
echo "Wrokflow Success"
if sh run_workflow2.sh #trigger workflow2
then
echo "Workflow2 Success"
exit_value=0
else
echo "Workflow2 Failed" >&2
fi
else
echo "Workflow Failed" >&2
fi
exit $exit_value
Note that I echo error messages to stderr (>&2). The exit command returns an error, which is an integer between 0-255. By convention we return 0 on success and 1 on error.
I also indented my code, which all experienced programmers do.
Related
I have below script which I will be running via jenkins:
#!/bin/bash
function getToken
{
echo "function to get token"
}
function call_init
{
echo "Creating a config file"
}
function call_list
{
echo "calling list*"
}
#Starting execution
if [[ -z "$TOKEN" ]]; then
TOKEN=$(getToken)
if [ $? -ne 0 ]; then
exit 1
fi
fi
echo "Creating a config file and populating it"
call_init
if [ $? -ne 0 ]; then
exit 1
fi
if [ -n $ACTION ]; then
case "$ACTION" in
'list') echo "Action is list"
call_list
if [ $? -ne 0 ]; then
exit 1
fi
;;
'update') echo "Section is update"
;;
'delete') echo "Section is delete"
;;
*) echo "This is a default message"
;;
esac
fi
As you see that theres a lot of repetition of the below code which helps me fail the jenkins job by throwing the error code 1:
if [ $? -ne 0 ]; then
exit 1
fi
What would be the most efficient way to handle this? I need it to always exit the code with 1.
P.S: I went through Checking Bash exit status of several commands efficiently, however was not able to get it work for the above script.
The best approach is to use explicit error checking.
Your current pattern can be streamlined, the following are all equivelant:
run_command
if [ $? -ne 0 ]; then
print_error
exit 1
fi
if ! run_command; then
print_error
exit 1
fi
run_command || { print_error; exit 1; }
Or in its simplest form, with no error message:
run_command || exit 1
As an alternative, you might want to use set -e.
You might also be interested in set -o pipefail.
These are not the preferred solution, as #William has pointed out, but can be useful for getting simple scripts to throw errors:
Note that set -e is generally not considered best practice. It's semantics are extremely unexpected in the edge cases (eg, if you invoke set -e in a function), and more importantly have changed dramatically with different versions of the shell. It is far better to explicitly invoke exit by running cmd || exit
You can use set -e to cause bash to bail out if a command returns non-zero, and set +e to disable this behaviour.
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:
[...]
-e Exit immediately if a command exits with a non-zero status.
[...]
-o option-name
Set the variable corresponding to option-name:
[...]
pipefail the return value of a pipeline is the status of
the last command to exit with a non-zero status,
or zero if no command exited with a non-zero status
[...]
To make use of this, you must enable the option before it would be required.
For example:
# disable 'exit immediately' (the default)
set +e
echo "running false..."
false
echo "we're still running"
# enable 'exit immediately'
set -e
echo "running false..."
false
echo "this should never get printed"
set -o pipefail must be used in conjunction with set -e:
# enable 'exit immediately'
set -e
# disable 'pipefail' (the default)
set +o pipefail
echo "running false | true..."
false | true
echo "we're still running (only the last exit status is considered)"
# enable 'pipefail'
set -o pipefail
echo "running false | true..."
false | true
echo "this should never get printed"
I have a simple shell script which I want to set up as a periodic Jenkins job rather than a cronjob for visibility and usability for less experienced users.
Here is the script:
#!/bin/bash
outputfile=/opt/jhc/streaming/check_error_output.txt
if [ "grep -sq 'Unable' $outputfile" == "0" ]; then
echo -e "ERROR MESSAGE FOUND\n"
exit 1
else
echo -e "NO ERROR MESSAGES HAVE BEEN FOUND\n"
exit 0
fi
My script will always return "NO ERROR MESSAGES HAVE BEEN FOUND" regardless of whether or not 'Unable' is in $outputfile, what am I doing wrong?
I also need my Jenkins job to class this as a success if 'Unable' isn't found (e.g. If script returns "0" then pass, everything else is fail)
Execute the grep command and check the exit status instead:
#!/bin/bash
outputfile=/opt/jhc/streaming/check_error_output.txt
grep -sq 'Unable' $outputfile
if [ "$?" == "0" ]; then
echo -e "ERROR MESSAGE FOUND\n"
exit 1
else
echo -e "NO ERROR MESSAGES HAVE BEEN FOUND\n"
exit 0
fi
You are comparing two different strings. The outcome will always be false, i.e. the else part is taken.
Also, no need to explicitly query the status code. Do it like this:
if grep -sq 'Unable' $outputfile
then
....
else
....
fi
This question already has answers here:
Automatic exit from Bash shell script on error [duplicate]
(8 answers)
Closed 6 years ago.
#Example Script
wget http://file1.com
cd /dir
wget http://file2.com
wget http://file3.com
I want to execute the bash script line by line and test the exit code ($?) of each execution and determine whether to proceed or not:
It basically means I need to add the following script below every line in the original script:
if test $? -eq 0
then
echo "No error"
else
echo "ERROR"
exit
fi
and the original script becomes:
#Example Script
wget http://file1.com
if test $? -eq 0
then
echo "No error"
else
echo "ERROR"
exit
fi
cd /dir
if test $? -eq 0
then
echo "No error"
else
echo "ERROR"
exit
fi
wget http://file2.com
if test $? -eq 0
then
echo "No error"
else
echo "ERROR"
exit
fi
wget http://file3.com
if test $? -eq 0
then
echo "No error"
else
echo "ERROR"
exit
fi
But the script becomes bloated.
Is there a better method?
One can use set -e but it's not without it's own pitfalls. Alternative one can bail out on errors:
command || exit 1
And an your if-statement can be written less verbose:
if command; then
The above is the same as:
command
if test "$?" -eq 0; then
set -e makes the script fail on non-zero exit status of any command. set +e removes the setting.
There are many ways to do that.
For example can use set in order to automatically stop on "bad" rc; simply by putting
set -e
on top of your script. Alternatively, you could write a "check_rc" function; see here for some starting points.
Or, you start with this:
check_error () {
if [ $RET == 0 ]; then
echo "DONE"
echo ""
else
echo "ERROR"
exit 1
fi
}
To be used with:
echo "some example command"
RET=$? ; check_error
As said; many ways to do this.
Best bet is to use set -e to terminate the script as soon as any non-zero return code is observed. Alternatively you can write a function to deal with error traps and call it after every command, this will reduce the if...else part and you can print any message before exiting.
trap errorsRead ERR;
function errorsRead() {
echo "Some none-zero return code observed..";
exit 1;
}
somecommand #command of your need
errorsRead # calling trap handling function
You can do this contraption:
wget http://file1.com || exit 1
This will terminate the script with error code 1 if a command returns a non-zero (failed) result.
Say I have two scripts that just print back the return code from a useless subscript:
script1
(echo; exit 0)
echo $?
script2
(echo)
echo $?
Both give back 0. But is there a way to tell that the first subscript explicitly uses the exit command?
After some research I got some breakthrough. Namely you can setup an exit_handler that can tell if there was an exit call by simply examining the last command.
#! /bin/bash
exit_handler () {
ret=$?
if echo "$BASH_COMMAND" | grep -e "^exit " >> /dev/null
then
echo "it was an explicit exit"
else
echo "it was an implicit exit"
fi
exit $ret
}
trap "exit_handler" EXIT
exit 22
This will print
it was an explicit exit
Now in order to tell the parent, instead of echoing, we can rather write to a file, a named pipe or whatever.
As per noting of choroba, exit without an argument will give implicit call, which is admittedly wrong since exit (without argument) is the same as exit $?. For that reason the regex has to take that into consideration:
#! /bin/bash
exit_handler () {
ret=$?
if echo "$BASH_COMMAND" | grep -e "^exit \|^exit$" >> /dev/null
then
echo "it was an explicit exit"
else
echo "it was an implicit exit"
fi
exit $ret
}
trap "exit_handler" EXIT
exit 22
So I found out that bash does not handle exceptions (there is no try/catch).
For my script, I would like to know if a command was successful or not.
This is the part of my code right now:
command = "scp -p$port $user:$password#$host:$from $to"
$command 2>/dev/null
if (( $? == 0 )); then
echo 'command was successful'
else
echo 'damn, there was an error'
fi
The things I don't understand are:
line 3, why do I have to put the 2 behind the $command?
line 5, what exactly is it with this $?
$? means the return code of the last executed command.
2> means redirecting the stderr (standard error stream) output to /dev/null.
Just FYI, this will also work:
if some_command 2>/dev/null ; then
echo 'command was successful'
else
echo 'damn, there was an error'
fi