Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if $? is not equal to zero in unix shell scripting?

Tags:

shell

unix

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.

like image 432
user1466466 Avatar asked Mar 18 '13 06:03

user1466466


People also ask

What is $? == 0 in shell script?

$? 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.

How do you check if a variable is zero in shell script?

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.

How do you check if not equal to in UNIX?

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.

What is role of $0 $? And $# in shell scripting?

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.


5 Answers

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.

like image 139
mtk Avatar answered Sep 23 '22 08:09

mtk


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
like image 31
Dennis Estenson Avatar answered Sep 22 '22 08:09

Dennis Estenson


Try this after execution of your script :

if [ $? -ne 0 ];
then
//statements//
fi
like image 39
Yash Khare Avatar answered Sep 25 '22 08:09

Yash Khare


I don't know how you got a string in $? but you can do:

if [[ "x$?" == "x0" ]]; then
   echo good
fi
like image 32
perreal Avatar answered Sep 24 '22 08:09

perreal


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"
like image 3
leonard Avatar answered Sep 22 '22 08:09

leonard