I'm testing about exit codes in bash and I coded the following script:
read -p "Path: " path
dr $path 2> /dev/null
echo "Command output level: "$?
if [ $? = 0 ]
then
echo "Command success"
elif [ $? = 127 ]
then
echo "Command not found"
else
echo "Command failed or not found"
fi
Now, I've been doing some research and I want to know if there's a way to make the very last "echo" avoid changing the exit code, if there's any I haven't found it.
I understand that the exit code is changed from 127 (yes, dr is on purpose to provoke the exit code) to 0 when I executed it.
Every command sets $?. If you wish to preserve the exit status of a specific command, you must save the value immediately.
dr $path 2> /dev/null
dr_status=$?
echo "Command output level: $dr_status"
if [ $dr_status = 0 ]
then
echo "Command success"
elif [ $dr_status = 127 ]
then
echo "Command not found"
else
echo "Command failed or not found"
fi
However, you can sometimes avoid repeated commands that would reset $?. For example,
dr $path 2> /dev/null
case $? in
0) echo "Command success" ;;
127) echo "Command not found" ;;
*) echo "Command failed or not found" ;;
esac
Related
I have this script here:
killall -q $1
#turns out system already has a method for this, yey
#-q because we already have a error handling
returned=$?
#does this so that it doesn't read the success state of the ifs
if [ $returned = 0 ]
then
printf "Process kill attempt returned "
echo $returned
echo "Process killed sucessfully."
#yey we did it
else
#oh noes it failed
printf "Process kill attempt returned "
echo $returned
echo "Process kill attempt failed."
printf "Most likely cause of failure: "
if [ $returned = 1 ]
then
echo "process does not exist or is not running"
elif [ $returned = 2 ]
echo "process is system task; insufficient permissions"
else
echo "unknown failure " $returned "; no known fail in database has this value"
fi
fi
When looking at it, I see no problem, yet when run, I get this error.
nathan#HAL-LINUX:~$ xscreensaver
nathan#HAL-LINUX:~$ killproc xscreensaver
/usr/local/bin/killproc: line 23: syntax error near unexpected token `else'
/usr/local/bin/killproc: line 23: ` else'
nathan#HAL-LINUX:~$
My original script omitted the nested if, by just straight up telling you it failed with error 3 or 1 or 2.
Should I go back? Or is there a way to fix this?
You forgot the then after your elif:
if [ $returned = 1 ]
then
echo "process does not exist or is not running"
elif [ $returned = 2 ]
then
echo "process is system task; insufficient permissions"
else
echo "unknown failure " $returned "; no known fail in database has this value"
fi
I want to check if last command failed or not in bash. I base this mini script on this
#!/bin/bash
mkdir nothere/cantcreate
echo $?
if [ $? -eq 0 ]; then
echo "command succeed"
else
echo "command failed"
fi
This prints the following:
mkdir: cannot create directory ‘nothere/cantcreate’: No such file or
directory
1
command succeed
I expect it to print command failed as the value of $? is 1. Why does the equality not behave as I expect ?
As mentioned in comment section, when you get to the if-clause $? evaluates to the exit code of echo $?.
The most straightforward and fool-proof way is to put the command itself in the if-clause:
#!/bin/bash
if mkdir nothere/cantcreate; then
echo "command succeed"
else
echo "command failed"
fi
echo $? itself is command that succeeds printing exit code of failed mkdir. If you want to capture exit code of mkdir you need to store it right after command call.
#!/bin/bash
mkdir nothere/cantcreate
RESULT=$?
echo $RESULT
if [ $RESULT -eq 0 ]; then
echo "command succeed"
else
echo "command failed"
fi
What would be the best way to check the exit status in an if statement in order to echo a specific output?
I'm thinking of it being:
if [ $? -eq 1 ]
then
echo "blah blah blah"
fi
The issue I am also having is that the exit statement is before the if statement simply because it has to have that exit code. Also, I know I'm doing something wrong since the exit would obviously exit the program.
Every command that runs has an exit status.
That check is looking at the exit status of the command that finished most recently before that line runs.
If you want your script to exit when that test returns true (the previous command failed) then you put exit 1 (or whatever) inside that if block after the echo.
That being said, if you are running the command and are wanting to test its output, using the following is often more straightforward.
if some_command; then
echo command returned true
else
echo command returned some error
fi
Or to turn that around use ! for negation
if ! some_command; then
echo command returned some error
else
echo command returned true
fi
Note though that neither of those cares what the error code is. If you know you only care about a specific error code then you need to check $? manually.
Note that exit codes != 0 are used to report errors. So, it's better to do:
retVal=$?
if [ $retVal -ne 0 ]; then
echo "Error"
fi
exit $retVal
instead of
# will fail for error codes == 1
retVal=$?
if [ $retVal -eq 1 ]; then
echo "Error"
fi
exit $retVal
An alternative to an explicit if statement
Minimally:
test $? -eq 0 || echo "something bad happened"
Complete:
EXITCODE=$?
test $EXITCODE -eq 0 && echo "something good happened" || echo "something bad happened";
exit $EXITCODE
$? is a parameter like any other. You can save its value to use before ultimately calling exit.
exit_status=$?
if [ $exit_status -eq 1 ]; then
echo "blah blah blah"
fi
exit $exit_status
For the record, if the script is run with set -e (or #!/bin/bash -e) and you therefore cannot check $? directly (since the script would terminate on any return code other than zero), but want to handle a specific code, #gboffis comment is great:
/some/command || error_code=$?
if [ "${error_code}" -eq 2 ]; then
...
Just to add to the helpful and detailed answer:
If you have to check the exit code explicitly, it is better to use the arithmetic operator, (( ... )), this way:
run_some_command
(($? != 0)) && { printf '%s\n' "Command exited with non-zero"; exit 1; }
Or, use a case statement:
run_some_command; ec=$? # grab the exit code into a variable so that it can
# be reused later, without the fear of being overwritten
case $ec in
0) ;;
1) printf '%s\n' "Command exited with non-zero"; exit 1;;
*) do_something_else;;
esac
Related answer about error handling in Bash:
Raise error in a Bash script
If you are writing a function – which is always preferred – you can propagate the error like this:
function()
{
if <command>; then
echo worked
else
return
fi
}
Now, the caller can do things like function && next as expected! This is useful if you have a lot of things to do in the if block, etc. (otherwise there are one-liners for this). It can easily be tested using the false command.
Using Z shell (zsh) you can simply use:
if [[ $(false)? -eq 1 ]]; then echo "yes" ;fi
When using Bash and set -e is on, you can use:
false || exit_code=$?
if [[ ${exit_code} -ne 0 ]]; then echo ${exit_code}; fi
This might only be useful in a limited set of use-cases, I use this specifically when I need to capture the output from a command and write it to a log file if the exit code reports that something went wrong.
RESULT=$(my_command_that_might_fail)
if (exit $?)
then
echo "everything went fine."
else
echo "ERROR: $RESULT" >> my_logfile.txt
fi
you can just add this if statement:
if [ $? -ne 0 ];
then
echo 'The previous command was not executed successfully';
fi
Below test scripts below work for
simple bash test commands
multiple test commands
bash test commands with pipe included:
if [[ $(echo -en "abc\n def" |grep -e "^abc") && ! $(echo -en "abc\n def" |grep -e "^def") ]] ; then
echo "pipe true"
else
echo "pipe false"
fi
if [[ $(echo -en "abc\n def" |grep -e "^abc") && $(echo -en "abc\n def" |grep -e "^def") ]] ; then
echo "pipe true"
else
echo "pipe false"
fi
The output is:
pipe true
pipe false
Here's my script:
if [ awk '$0 ~ /Failed/ { print }' $(pwd)/unity.log ]; then
echo "Build Failed"
exit 1
else
echo "Build Success"
exit 0
fi
The gist is that I am checking the file for "Build Failed" message and exiting 1 if failed.
If what I understand is correct, awk will return blank string if there is no text in the file and some text if it's found. But it shoots syntax error : ./Scripts/build.sh: line 41: [: too many arguments
Remove the [], use grep:
if grep -qw Failed unity.log; then
echo "Build Failed"
exit 1
else
echo "Build Success"
exit 0
fi
You could take advantage of the default exit value being 0. Also, there's no reason to do $(pwd)/filename; just do filename.
grep -qw Failed unity.log || { echo "Build Failed"; exit 1; }
echo "Build Success"
It would be also more idiomatic to send the error message on failure to stderr (with echo >&2).
try this:
if [ ! "$(grep 'Build Failed' unity.log)" ]; then
exit 1
fi
The exit statements in each status check if statement do not break the while loop and truly exit the script. Is there something I can do to break the loop and exit with that $STATUS code?
EDIT: I've updated my code and it still isn't working. The status check if statements successfully break the loop but when I try to evaluate the $EXIT_STATUS it's always null, likely having something to do with scope. What am I missing here?
if [ $RESTART -le $STEP ]; then
. tell_step
while read XML_INPUT; do
XML_GDG=`get_full_name $GDG_NAME P`
cp $XML_INPUT $XML_GDG
STATUS=$?
EXIT_STATUS=$STATUS
if [ $STATUS -ne 0 ]; then
break
fi
add_one_gen $XML_GDG
STATUS=$?
EXIT_STATUS=$STATUS
if [ $STATUS -ne 0 ]; then
break
fi
done < $XML_STAGE_LIST
echo $EXIT_STATUS
if [ $EXIT_STATUS -ne 0 ]; then
exit $EXIT_STATUS
fi
fi
I had the same problem: when piping into a while loop, the script did not exit on exit. Instead it worked like "break" should do.
I have found 2 solutions:
a) After your while loop check the return code of the while loop and exit then:
somecommand | while something; do
...
done
# pass the exit code from the while loop
if [ $? != 0 ]
then
# this really exits
exit $?
fi
b) Set the bash script to exit on any error. Paste this at the beginning of your script:
set -e
Not really understand why your script dosn't exits on exit, because the next is works without problems:
while read name; do
echo "checking: $name"
grep $name /etc/passwd >/dev/null 2>&1
STATUS=$?
if [ $STATUS -ne 0 ]; then
echo "grep failed for $name rc-$STATUS"
exit $STATUS
fi
done <<EOF
root
bullshit
daemon
EOF
running it, produces:
$ bash testscript.sh ; echo "exited with: $?"
grep failed for bullshit rc-1
exited with: 1
as you can see, the script exited immediatelly and doesn't check the "daemon".
Anyway, maybe it is more readable, when you will use bash functions like:
dostep1() {
grep "$1:" /etc/passwd >/dev/null 2>&1
return $?
}
dostep2() {
grep "$1:" /some/nonexistent/file >/dev/null 2>&1
return $?
}
err() {
retval=$1; shift;
echo "$#" >&2 ; return $retval
}
while read name
do
echo =checking $name=
dostep1 $name || err $? "Step 1 failed" || exit $?
dostep2 $name || err $? "Step 2 failed" || exit $?
done
when run like:
echo 'root
> bullshit' | bash testexit.sh; echo "status: $?"
=checking root=
Step 2 failed
status: 2
so, step1 was OK and exited on the step2 (nonexisten file) - grep exit status 2, and when
echo 'bullshit
bin' | bash testexit.sh; echo "status: $?"
=checking bullshit=
Step 1 failed
status: 1
exited immediatelly on step1 (bullshit isn't in /etc/passwd) - grep exit status 1
You'll need to break out of your loop and then exit from your script. You can use a variable which is set on error to test if you need to exit with an error condition.
I had a similar problem when pipelining. My guess is a separate shell is started when piplining. Hopefully it helps someone else who stumbles across the problem.
From jm666's post above, this will not print 'Here I am!':
while read name; do
echo "checking: $name"
grep $name /etc/passwd >/dev/null 2>&1
STATUS=$?
if [ $STATUS -ne 0 ]; then
echo "grep failed for $name rc-$STATUS"
exit $STATUS
fi
done <<EOF
root
yayablah
daemon
EOF
echo "Here I am!"
However the following, which pipes the names to the while loop, does. It will also exit with a code of 0. Setting the variable and breaking doesn't seem to work either (which makes sense if it is another shell). Another method needs to be used to either communicate the error or avoid the situation in the first place.
cat <<EOF |
root
yayablah
daemon
EOF
while read name; do
echo "checking: $name"
grep $name /etc/passwd >/dev/null 2>&1
STATUS=$?
if [ $STATUS -ne 0 ]; then
echo "grep failed for $name rc-$STATUS"
exit $STATUS
fi
done
echo "Here I am!"