How to avoid a bash script from failing when -e option is set? - bash

I have a bash script with -e option set, which fails the whole script on the very first error.
In the script, I am trying to do an ls on a directory. But that path may or may not exist. If the path does not exist, the ls command fails, since the -e flag is set.
Is there a way by which I can prevent the script from failing?
As a side note, I have tried the trick to do an set +e and set -e before and after that command and it works. But I am looking for some better solution.

You can "catch" the error using || and a command guaranteed to exit with 0 status:
ls $PATH || echo "$PATH does not exist"
Since the compound command succeeds whether or not $PATH exists, set -e is not triggered and your script will not exit.
To suppress the error silently, you can use the true command:
ls $PATH || true
To execute multiple commands, you can use one of the compound commands:
ls $PATH || { command1; command2; }
or
ls $PATH || ( command1; command2 )
Just be sure nothing fails inside either compound command, either. One benefit of the second example is that you can turn off immediate-exit mode inside the subshell without affecting its status in the current shell:
ls $PATH || ( set +e; do-something-that-might-fail )

Another option is to use trap to catch the EXIT signal:
trap 'echo "ls failed" ; some_rescue_action' EXIT
ls /non_exist

one solution would be testing the existence of the folder
function myLs() {
LIST=""
folder=$1
[ "x$folder" = "x" ] && folder="."
[ -d $folder ] && LIST=`ls $folder`
echo $LIST
}
This way bash won't fail if $folder does not exist

Related

How to exit gitlab job when script fails [duplicate]

I have a Bash shell script that invokes a number of commands.
I would like to have the shell script automatically exit with a return value of 1 if any of the commands return a non-zero value.
Is this possible without explicitly checking the result of each command?
For example,
dosomething1
if [[ $? -ne 0 ]]; then
exit 1
fi
dosomething2
if [[ $? -ne 0 ]]; then
exit 1
fi
Add this to the beginning of the script:
set -e
This will cause the shell to exit immediately if a simple command exits with a nonzero exit value. A simple command is any command not part of an if, while, or until test, or part of an && or || list.
See the bash manual on the "set" internal command for more details.
It's really annoying to have a script stubbornly continue when something fails in the middle and breaks assumptions for the rest of the script. I personally start almost all portable shell scripts with set -e.
If I'm working with bash specifically, I'll start with
set -Eeuo pipefail
This covers more error handling in a similar fashion. I consider these as sane defaults for new bash programs. Refer to the bash manual for more information on what these options do.
To add to the accepted answer:
Bear in mind that set -e sometimes is not enough, specially if you have pipes.
For example, suppose you have this script
#!/bin/bash
set -e
./configure > configure.log
make
... which works as expected: an error in configure aborts the execution.
Tomorrow you make a seemingly trivial change:
#!/bin/bash
set -e
./configure | tee configure.log
make
... and now it does not work. This is explained here, and a workaround (Bash only) is provided:
#!/bin/bash
set -e
set -o pipefail
./configure | tee configure.log
make
The if statements in your example are unnecessary. Just do it like this:
dosomething1 || exit 1
If you take Ville Laurikari's advice and use set -e then for some commands you may need to use this:
dosomething || true
The || true will make the command pipeline have a true return value even if the command fails so the the -e option will not kill the script.
If you have cleanup you need to do on exit, you can also use 'trap' with the pseudo-signal ERR. This works the same way as trapping INT or any other signal; bash throws ERR if any command exits with a nonzero value:
# Create the trap with
# trap COMMAND SIGNAME [SIGNAME2 SIGNAME3...]
trap "rm -f /tmp/$MYTMPFILE; exit 1" ERR INT TERM
command1
command2
command3
# Partially turn off the trap.
trap - ERR
# Now a control-C will still cause cleanup, but
# a nonzero exit code won't:
ps aux | grep blahblahblah
Or, especially if you're using "set -e", you could trap EXIT; your trap will then be executed when the script exits for any reason, including a normal end, interrupts, an exit caused by the -e option, etc.
The $? variable is rarely needed. The pseudo-idiom command; if [ $? -eq 0 ]; then X; fi should always be written as if command; then X; fi.
The cases where $? is required is when it needs to be checked against multiple values:
command
case $? in
(0) X;;
(1) Y;;
(2) Z;;
esac
or when $? needs to be reused or otherwise manipulated:
if command; then
echo "command successful" >&2
else
ret=$?
echo "command failed with exit code $ret" >&2
exit $ret
fi
Run it with -e or set -e at the top.
Also look at set -u.
On error, the below script will print a RED error message and exit.
Put this at the top of your bash script:
# BASH error handling:
# exit on command failure
set -e
# keep track of the last executed command
trap 'LAST_COMMAND=$CURRENT_COMMAND; CURRENT_COMMAND=$BASH_COMMAND' DEBUG
# on error: print the failed command
trap 'ERROR_CODE=$?; FAILED_COMMAND=$LAST_COMMAND; tput setaf 1; echo "ERROR: command \"$FAILED_COMMAND\" failed with exit code $ERROR_CODE"; put sgr0;' ERR INT TERM
An expression like
dosomething1 && dosomething2 && dosomething3
will stop processing when one of the commands returns with a non-zero value. For example, the following command will never print "done":
cat nosuchfile && echo "done"
echo $?
1
#!/bin/bash -e
should suffice.
I am just throwing in another one for reference since there was an additional question to Mark Edgars input and here is an additional example and touches on the topic overall:
[[ `cmd` ]] && echo success_else_silence
Which is the same as cmd || exit errcode as someone showed.
For example, I want to make sure a partition is unmounted if mounted:
[[ `mount | grep /dev/sda1` ]] && umount /dev/sda1

Continue running npm test script after test fails in a bash loop [duplicate]

I have a bash script with -e option set, which fails the whole script on the very first error.
In the script, I am trying to do an ls on a directory. But that path may or may not exist. If the path does not exist, the ls command fails, since the -e flag is set.
Is there a way by which I can prevent the script from failing?
As a side note, I have tried the trick to do an set +e and set -e before and after that command and it works. But I am looking for some better solution.
You can "catch" the error using || and a command guaranteed to exit with 0 status:
ls $PATH || echo "$PATH does not exist"
Since the compound command succeeds whether or not $PATH exists, set -e is not triggered and your script will not exit.
To suppress the error silently, you can use the true command:
ls $PATH || true
To execute multiple commands, you can use one of the compound commands:
ls $PATH || { command1; command2; }
or
ls $PATH || ( command1; command2 )
Just be sure nothing fails inside either compound command, either. One benefit of the second example is that you can turn off immediate-exit mode inside the subshell without affecting its status in the current shell:
ls $PATH || ( set +e; do-something-that-might-fail )
Another option is to use trap to catch the EXIT signal:
trap 'echo "ls failed" ; some_rescue_action' EXIT
ls /non_exist
one solution would be testing the existence of the folder
function myLs() {
LIST=""
folder=$1
[ "x$folder" = "x" ] && folder="."
[ -d $folder ] && LIST=`ls $folder`
echo $LIST
}
This way bash won't fail if $folder does not exist

Abort bash script if git pull fails [duplicate]

I have a Bash shell script that invokes a number of commands.
I would like to have the shell script automatically exit with a return value of 1 if any of the commands return a non-zero value.
Is this possible without explicitly checking the result of each command?
For example,
dosomething1
if [[ $? -ne 0 ]]; then
exit 1
fi
dosomething2
if [[ $? -ne 0 ]]; then
exit 1
fi
Add this to the beginning of the script:
set -e
This will cause the shell to exit immediately if a simple command exits with a nonzero exit value. A simple command is any command not part of an if, while, or until test, or part of an && or || list.
See the bash manual on the "set" internal command for more details.
It's really annoying to have a script stubbornly continue when something fails in the middle and breaks assumptions for the rest of the script. I personally start almost all portable shell scripts with set -e.
If I'm working with bash specifically, I'll start with
set -Eeuo pipefail
This covers more error handling in a similar fashion. I consider these as sane defaults for new bash programs. Refer to the bash manual for more information on what these options do.
To add to the accepted answer:
Bear in mind that set -e sometimes is not enough, specially if you have pipes.
For example, suppose you have this script
#!/bin/bash
set -e
./configure > configure.log
make
... which works as expected: an error in configure aborts the execution.
Tomorrow you make a seemingly trivial change:
#!/bin/bash
set -e
./configure | tee configure.log
make
... and now it does not work. This is explained here, and a workaround (Bash only) is provided:
#!/bin/bash
set -e
set -o pipefail
./configure | tee configure.log
make
The if statements in your example are unnecessary. Just do it like this:
dosomething1 || exit 1
If you take Ville Laurikari's advice and use set -e then for some commands you may need to use this:
dosomething || true
The || true will make the command pipeline have a true return value even if the command fails so the the -e option will not kill the script.
If you have cleanup you need to do on exit, you can also use 'trap' with the pseudo-signal ERR. This works the same way as trapping INT or any other signal; bash throws ERR if any command exits with a nonzero value:
# Create the trap with
# trap COMMAND SIGNAME [SIGNAME2 SIGNAME3...]
trap "rm -f /tmp/$MYTMPFILE; exit 1" ERR INT TERM
command1
command2
command3
# Partially turn off the trap.
trap - ERR
# Now a control-C will still cause cleanup, but
# a nonzero exit code won't:
ps aux | grep blahblahblah
Or, especially if you're using "set -e", you could trap EXIT; your trap will then be executed when the script exits for any reason, including a normal end, interrupts, an exit caused by the -e option, etc.
The $? variable is rarely needed. The pseudo-idiom command; if [ $? -eq 0 ]; then X; fi should always be written as if command; then X; fi.
The cases where $? is required is when it needs to be checked against multiple values:
command
case $? in
(0) X;;
(1) Y;;
(2) Z;;
esac
or when $? needs to be reused or otherwise manipulated:
if command; then
echo "command successful" >&2
else
ret=$?
echo "command failed with exit code $ret" >&2
exit $ret
fi
Run it with -e or set -e at the top.
Also look at set -u.
On error, the below script will print a RED error message and exit.
Put this at the top of your bash script:
# BASH error handling:
# exit on command failure
set -e
# keep track of the last executed command
trap 'LAST_COMMAND=$CURRENT_COMMAND; CURRENT_COMMAND=$BASH_COMMAND' DEBUG
# on error: print the failed command
trap 'ERROR_CODE=$?; FAILED_COMMAND=$LAST_COMMAND; tput setaf 1; echo "ERROR: command \"$FAILED_COMMAND\" failed with exit code $ERROR_CODE"; put sgr0;' ERR INT TERM
An expression like
dosomething1 && dosomething2 && dosomething3
will stop processing when one of the commands returns with a non-zero value. For example, the following command will never print "done":
cat nosuchfile && echo "done"
echo $?
1
#!/bin/bash -e
should suffice.
I am just throwing in another one for reference since there was an additional question to Mark Edgars input and here is an additional example and touches on the topic overall:
[[ `cmd` ]] && echo success_else_silence
Which is the same as cmd || exit errcode as someone showed.
For example, I want to make sure a partition is unmounted if mounted:
[[ `mount | grep /dev/sda1` ]] && umount /dev/sda1

Getting status when launching an alias in bash

I've figured out how to use aliases from within a bash script. They do get executed, but I don't get the result back in the variable "Status". All following types of launching an alias get the result back when used interactively, but "Status" stay empty in the script no matter if I use "shopt -s expand_aliases" or not:
$ Status=`start_ETAP-TestCluster 2>/dev/null`
$ echo $Status
{"JobIds":["8c54392c-363c-4e28-827e-a1384da7fd37"]}
$ Status=`${BASH_ALIASES[start_NetIntel-Cluster]} 2>/dev/null`
$ echo $Status
{"JobIds":["dd05f5cb-a490-4fe3-b4fd-be61f62bbb88"]}
$ eval Status=`${BASH_ALIASES[start_NetIntel-Cluster]} 2>/dev/null`
$ echo $Status
{JobIds:[25a246ab-e5f5-4d65-80ea-4ff5e15cc02c]}
$ eval $(echo Status=`${BASH_ALIASES[start_NetIntel-Cluster]} 2>/dev/null`)
$ echo $Status
{JobIds:[4ce2f4e5-0599-47f5-b7f4-cd2ec1de9909]}
Has anyone some hints and ideas how to get the result in the variable "Status" within a bash script ?
The command between back quotes, or betwen $( and ) is expanded by the shell to the ouput of command. The exit status can be retrieved with $? special variable.
output=$(command ) # output=`command ` #old syntax
status=$?
Otherwise standard output and error can be redirected to files.
command 1> output_file 2> error_file
status=$?
Also if only the exit status success is wanted the status can be checked with logical operators (exit status ==0 means success and logical value true)
command 1> output_file 2> error_file || {
echo failed
exit 1
}
Thank you for your help and hints. The main problem was that expanding the alias (like start_ETAP-Cluster) did not work properly. Thus using following in the script did help:
shopt -s expand_aliases
source ~/.bash_aliases
Using the $Status in my code was misleading, I changed it to $JobID, which reflects better what I wanted. I'm using web hooks in the aliases, which return a JSON Object with the job id. Furthermore assigning $Status=$? works also. Now the code does its work from within the script:
$ JobID=$( ${BASH_ALIASES[start_ETAP-Cluster]} 2>/dev/null )
$ echo $JobID
{"JobIds":["8c54392c-363c-4e28-827e-a1384da7fd37"]}
Next step will be to move the web hook calls from the aliases to bash functions, will give me more flexibility then aliases.

"set -e" in shell and command substitution

In shell scripts set -e is often used to make them more robust by stopping the script when some of the commands executed from the script exits with non-zero exit code.
It's usually easy to specify that you don't care about some of the commands succeeding by adding || true at the end.
The problem appears when you actually care about the return value, but don't want the script to stop on non-zero return code, for example:
output=$(possibly-failing-command)
if [ 0 == $? -a -n "$output" ]; then
...
else
...
fi
Here we want to both check the exit code (thus we can't use || true inside of command substitution expression) and get the output. However, if the command in command substitution fails, the whole script stops due to set -e.
Is there a clean way to prevent the script from stopping here without unsetting -e and setting it back afterwards?
Yes, inline the process substitution in the if-statement
#!/bin/bash
set -e
if ! output=$(possibly-failing-command); then
...
else
...
fi
Command Fails
$ ( set -e; if ! output=$(ls -l blah); then echo "command failed"; else echo "output is -->$output<--"; fi )
/bin/ls: cannot access blah: No such file or directory
command failed
Command Works
$ ( set -e; if ! output=$(ls -l core); then echo "command failed"; else echo "output is: $output"; fi )
output is: -rw------- 1 siegex users 139264 2010-12-01 02:02 core

Resources