After each command, the exit code can be found in the $?
variable so you would have something like:
ls -al file.ext
rc=$?; if [[ $rc != 0 ]]; then exit $rc; fi
You need to be careful of piped commands since the $?
only gives you the return code of the last element in the pipe so, in the code:
ls -al file.ext | sed 's/^/xx: /"
will not return an error code if the file doesn't exist (since the sed
part of the pipeline actually works, returning 0).
The bash
shell actually provides an array which can assist in that case, that being PIPESTATUS
. This array has one element for each of the pipeline components, that you can access individually like ${PIPESTATUS[0]}
:
pax> false | true ; echo ${PIPESTATUS[0]}
1
Note that this is getting you the result of the false
command, not the entire pipeline. You can also get the entire list to process as you see fit:
pax> false | true | false; echo ${PIPESTATUS[*]}
1 0 1
If you wanted to get the largest error code from a pipeline, you could use something like:
true | true | false | true | false
rcs=${PIPESTATUS[*]}; rc=0; for i in ${rcs}; do rc=$(($i > $rc ? $i : $rc)); done
echo $rc
This goes through each of the PIPESTATUS
elements in turn, storing it in rc
if it was greater than the previous rc
value.
If you want to work with $?
, you'll need to check it after each command, since $?
is updated after each command exits. This means that if you execute a pipeline, you'll only get the exit code of the last process in the pipeline.
Another approach is to do this:
set -e
set -o pipefail
If you put this at the top of the shell script, it looks like Bash will take care of this for you. As a previous poster noted, "set -e" will cause Bash to exit with an error on any simple command. "set -o pipefail" will cause Bash to exit with an error on any command in a pipeline as well.
See here or here for a little more discussion on this problem. Here is the Bash manual section on the set
builtin.
If you want to work with $?
, you'll need to check it after each command, since $?
is updated after each command exits. This means that if you execute a pipeline, you'll only get the exit code of the last process in the pipeline.
Another approach is to do this:
set -e
set -o pipefail
If you put this at the top of the shell script, it looks like Bash will take care of this for you. As a previous poster noted, "set -e" will cause Bash to exit with an error on any simple command. "set -o pipefail" will cause Bash to exit with an error on any command in a pipeline as well.
See here or here for a little more discussion on this problem. Here is the Bash manual section on the set
builtin.
If you want to work with $?
, you'll need to check it after each command, since $?
is updated after each command exits. This means that if you execute a pipeline, you'll only get the exit code of the last process in the pipeline.
Another approach is to do this:
set -e
set -o pipefail
If you put this at the top of the shell script, it looks like Bash will take care of this for you. As a previous poster noted, "set -e" will cause Bash to exit with an error on any simple command. "set -o pipefail" will cause Bash to exit with an error on any command in a pipeline as well.
See here or here for a little more discussion on this problem. Here is the Bash manual section on the set
builtin.
If you want to work with $?
, you'll need to check it after each command, since $?
is updated after each command exits. This means that if you execute a pipeline, you'll only get the exit code of the last process in the pipeline.
Another approach is to do this:
set -e
set -o pipefail
If you put this at the top of the shell script, it looks like Bash will take care of this for you. As a previous poster noted, "set -e" will cause Bash to exit with an error on any simple command. "set -o pipefail" will cause Bash to exit with an error on any command in a pipeline as well.
See here or here for a little more discussion on this problem. Here is the Bash manual section on the set
builtin.
"set -e
" is probably the easiest way to do this. Just put that before any commands in your program.
"set -e
" is probably the easiest way to do this. Just put that before any commands in your program.
"set -e
" is probably the easiest way to do this. Just put that before any commands in your program.
"set -e
" is probably the easiest way to do this. Just put that before any commands in your program.
If you just call exit in Bash without any parameters, it will return the exit code of the last command. Combined with OR
, Bash should only invoke exit, if the previous command fails. But I haven't tested this.
command1 || exit; command2 || exit;
Bash will also store the exit code of the last command in the variable $?
.
If you just call exit in Bash without any parameters, it will return the exit code of the last command. Combined with OR
, Bash should only invoke exit, if the previous command fails. But I haven't tested this.
command1 || exit; command2 || exit;
Bash will also store the exit code of the last command in the variable $?
.
If you just call exit in Bash without any parameters, it will return the exit code of the last command. Combined with OR
, Bash should only invoke exit, if the previous command fails. But I haven't tested this.
command1 || exit; command2 || exit;
Bash will also store the exit code of the last command in the variable $?
.
If you just call exit in Bash without any parameters, it will return the exit code of the last command. Combined with OR
, Bash should only invoke exit, if the previous command fails. But I haven't tested this.
command1 || exit; command2 || exit;
Bash will also store the exit code of the last command in the variable $?
.
[ $? -eq 0 ] || exit $?; # Exit for nonzero return code
[ $? -eq 0 ] || exit $?; # Exit for nonzero return code
http://cfaj.freeshell.org/shell/cus-faq-2.html#11
How do I get the exit code of cmd1
in cmd1|cmd2
First, note that cmd1
exit code could be non-zero and still don't mean an error. This happens for instance in
cmd | head -1
You might observe a 141 (or 269 with ksh93) exit status of cmd1
, but it's because cmd
was interrupted by a SIGPIPE signal when head -1
terminated after having read one line.
To know the exit status of the elements of a pipeline
cmd1 | cmd2 | cmd3
a. with Z shell (zsh
):
The exit codes are provided in the pipestatus special array.
cmd1
exit code is in $pipestatus[1]
, cmd3
exit code in
$pipestatus[3]
, so that $?
is always the same as
$pipestatus[-1]
.
b. with Bash:
The exit codes are provided in the PIPESTATUS
special array.
cmd1
exit code is in ${PIPESTATUS[0]}
, cmd3
exit code in
${PIPESTATUS[2]}
, so that $?
is always the same as
${PIPESTATUS: -1}
.
...
For more details see Z shell.
http://cfaj.freeshell.org/shell/cus-faq-2.html#11
How do I get the exit code of cmd1
in cmd1|cmd2
First, note that cmd1
exit code could be non-zero and still don't mean an error. This happens for instance in
cmd | head -1
You might observe a 141 (or 269 with ksh93) exit status of cmd1
, but it's because cmd
was interrupted by a SIGPIPE signal when head -1
terminated after having read one line.
To know the exit status of the elements of a pipeline
cmd1 | cmd2 | cmd3
a. with Z shell (zsh
):
The exit codes are provided in the pipestatus special array.
cmd1
exit code is in $pipestatus[1]
, cmd3
exit code in
$pipestatus[3]
, so that $?
is always the same as
$pipestatus[-1]
.
b. with Bash:
The exit codes are provided in the PIPESTATUS
special array.
cmd1
exit code is in ${PIPESTATUS[0]}
, cmd3
exit code in
${PIPESTATUS[2]}
, so that $?
is always the same as
${PIPESTATUS: -1}
.
...
For more details see Z shell.
http://cfaj.freeshell.org/shell/cus-faq-2.html#11
How do I get the exit code of cmd1
in cmd1|cmd2
First, note that cmd1
exit code could be non-zero and still don't mean an error. This happens for instance in
cmd | head -1
You might observe a 141 (or 269 with ksh93) exit status of cmd1
, but it's because cmd
was interrupted by a SIGPIPE signal when head -1
terminated after having read one line.
To know the exit status of the elements of a pipeline
cmd1 | cmd2 | cmd3
a. with Z shell (zsh
):
The exit codes are provided in the pipestatus special array.
cmd1
exit code is in $pipestatus[1]
, cmd3
exit code in
$pipestatus[3]
, so that $?
is always the same as
$pipestatus[-1]
.
b. with Bash:
The exit codes are provided in the PIPESTATUS
special array.
cmd1
exit code is in ${PIPESTATUS[0]}
, cmd3
exit code in
${PIPESTATUS[2]}
, so that $?
is always the same as
${PIPESTATUS: -1}
.
...
For more details see Z shell.
http://cfaj.freeshell.org/shell/cus-faq-2.html#11
How do I get the exit code of cmd1
in cmd1|cmd2
First, note that cmd1
exit code could be non-zero and still don't mean an error. This happens for instance in
cmd | head -1
You might observe a 141 (or 269 with ksh93) exit status of cmd1
, but it's because cmd
was interrupted by a SIGPIPE signal when head -1
terminated after having read one line.
To know the exit status of the elements of a pipeline
cmd1 | cmd2 | cmd3
a. with Z shell (zsh
):
The exit codes are provided in the pipestatus special array.
cmd1
exit code is in $pipestatus[1]
, cmd3
exit code in
$pipestatus[3]
, so that $?
is always the same as
$pipestatus[-1]
.
b. with Bash:
The exit codes are provided in the PIPESTATUS
special array.
cmd1
exit code is in ${PIPESTATUS[0]}
, cmd3
exit code in
${PIPESTATUS[2]}
, so that $?
is always the same as
${PIPESTATUS: -1}
.
...
For more details see Z shell.
For Bash:
# This will trap any errors or commands with non-zero exit status
# by calling function catch_errors()
trap catch_errors ERR;
#
# ... the rest of the script goes here
#
function catch_errors() {
# Do whatever on errors
#
#
echo "script aborted, because of errors";
exit 0;
}
For Bash:
# This will trap any errors or commands with non-zero exit status
# by calling function catch_errors()
trap catch_errors ERR;
#
# ... the rest of the script goes here
#
function catch_errors() {
# Do whatever on errors
#
#
echo "script aborted, because of errors";
exit 0;
}
For Bash:
# This will trap any errors or commands with non-zero exit status
# by calling function catch_errors()
trap catch_errors ERR;
#
# ... the rest of the script goes here
#
function catch_errors() {
# Do whatever on errors
#
#
echo "script aborted, because of errors";
exit 0;
}
For Bash:
# This will trap any errors or commands with non-zero exit status
# by calling function catch_errors()
trap catch_errors ERR;
#
# ... the rest of the script goes here
#
function catch_errors() {
# Do whatever on errors
#
#
echo "script aborted, because of errors";
exit 0;
}
In Bash this is easy. Just tie them together with &&
:
command1 && command2 && command3
You can also use the nested if construct:
if command1
then
if command2
then
do_something
else
exit
fi
else
exit
fi
In Bash this is easy. Just tie them together with &&
:
command1 && command2 && command3
You can also use the nested if construct:
if command1
then
if command2
then
do_something
else
exit
fi
else
exit
fi
In Bash this is easy. Just tie them together with &&
:
command1 && command2 && command3
You can also use the nested if construct:
if command1
then
if command2
then
do_something
else
exit
fi
else
exit
fi
In Bash this is easy. Just tie them together with &&
:
command1 && command2 && command3
You can also use the nested if construct:
if command1
then
if command2
then
do_something
else
exit
fi
else
exit
fi
#
#------------------------------------------------------------------------------
# purpose: to run a command, log cmd output, exit on error
# usage:
# set -e; do_run_cmd_or_exit "$cmd" ; set +e
#------------------------------------------------------------------------------
do_run_cmd_or_exit(){
cmd="$@" ;
do_log "DEBUG running cmd or exit: \"$cmd\""
msg=$($cmd 2>&1)
export exit_code=$?
# If occurred during the execution, exit with error
error_msg="Failed to run the command:
\"$cmd\" with the output:
\"$msg\" !!!"
if [ $exit_code -ne 0 ] ; then
do_log "ERROR $msg"
do_log "FATAL $msg"
do_exit "$exit_code" "$error_msg"
else
# If no errors occurred, just log the message
do_log "DEBUG : cmdoutput : \"$msg\""
fi
}
#
#------------------------------------------------------------------------------
# purpose: to run a command, log cmd output, exit on error
# usage:
# set -e; do_run_cmd_or_exit "$cmd" ; set +e
#------------------------------------------------------------------------------
do_run_cmd_or_exit(){
cmd="$@" ;
do_log "DEBUG running cmd or exit: \"$cmd\""
msg=$($cmd 2>&1)
export exit_code=$?
# If occurred during the execution, exit with error
error_msg="Failed to run the command:
\"$cmd\" with the output:
\"$msg\" !!!"
if [ $exit_code -ne 0 ] ; then
do_log "ERROR $msg"
do_log "FATAL $msg"
do_exit "$exit_code" "$error_msg"
else
# If no errors occurred, just log the message
do_log "DEBUG : cmdoutput : \"$msg\""
fi
}
Source: Stackoverflow.com