I have a script which uses test command to check if $?
(return code of last executed command) is not equal to zero. The code is as follows: -
$?
is the exit status of the last command executed.
if (test $? -ne 0)
then
//statements//
fi
However this way of validation does not work for strings as get syntax error . Please suggest a suitable alternative to this.
$? is the exit status of the most recently-executed command; by convention, 0 means success and anything else indicates failure. That line is testing whether the grep command succeeded.
You can verify this using the below tests which all evaluate to true because xyz is either empty or unset: if [ -z ] ; then echo "true"; else echo "false"; fi. xyz=""; if [ -z "$xyz" ] ; then echo "true"; else echo "false"; fi.
Checks if the value of two operands are equal or not; if values are not equal then the condition becomes true. [ $a != $b ] is true.
If you execute ./script.sh , $0 will give output ./script.sh but if you execute it with bash script.sh it will give output script.sh . Show activity on this post. They are called the Positional Parameters.
Put it in a variable first and then try to test it, as shown below
ret=$?
if [ $ret -ne 0 ]; then
echo "In If"
else
echo "In Else"
fi
This should help.
Edit: If the above is not working as expected then, there is a possibility that you are not using $?
at right place. It must be the very next line after the command of which you need to catch the return status. Even if there is any other single command in between the target and you catching it's return status, you'll be retrieving the returns_status of this intermediate command and not the one you are expecting.
You don't need to test if $?
is not 0
. The shell provides &&
and ||
so you can easily branch based on implicit result of that test:
some_command && {
# executes this block of code,
# if some_command would result in: $? -eq 0
} || {
# executes this block of code,
# if some_command would result in: $? -ne 0
}
You can remove either branch, depending on what you want. So if you just want to test for failure (i.e. $? -ne 0
):
some_command_returning_nonzero || {
# executes this block of code when: $? -ne 0
# and nothing if the command succeeds: $? -eq 0
}
However, the code you provided in the question works, as is. I'm confused that you got syntax errors & concluded that $?
was a string. It's most likely that the errant code causing the syntax error was not provided with the question. This is especially evident because you claim that no one else's solutions work either. When this happens, you have to re-evaluate your assumptions.
NB: The code above may give confusing results if the code inside the braces returns an error. In that case simply use the if
command instead, like this:
if some_command; then
# executes this block of code,
# if some_command would result in: $? -eq 0
else
# executes this block of code,
# if some_command would result in: $? -ne 0
fi
Try this after execution of your script :
if [ $? -ne 0 ];
then
//statements//
fi
I don't know how you got a string in $?
but you can do:
if [[ "x$?" == "x0" ]]; then
echo good
fi
This is a solution that came up with for a similar issue
exit_status () {
if [ $? = 0 ]
then
true
else
false
fi
}
usage:
do-command exit_status && echo "worked" || echo "didnt work"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With