简体   繁体   English

如何检查是否$? 在 unix shell 脚本中不等于零?

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

I have a script which uses test command to check if $?我有一个脚本,它使用 test 命令来检查$? (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 .然而,这种验证方式不适用于字符串,因为 get syntax error 。 Please suggest a suitable alternative to this.请提出一个合适的替代方案。

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.即使目标之间有任何其他单个命令并且您捕获它的返回状态,您也将检索此中间命令的returns_status,而不是您期望的那个。

You don't need to test if $?你不需要测试是否$? is not 0 .不是0 The shell provides && and || shell 提供&&|| 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 (ie $? -ne 0 ):因此,如果您只想测试失败(即$? -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 命令,如下所示:

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"
<run your last command on this line>
a=${?}
if [ ${a} -ne 0 ]; then echo "do something"; fi

use whatever command you want to use instead of the echo "do something" command使用您想使用的任何命令,而不是echo "do something"命令

if [ $var1 != $var2 ] 
then 
echo "$var1"
else 
echo "$var2"
fi

I put together some code that may help to see how return value vs returned strings works.我整理了一些可能有助于了解返回值与返回字符串如何工作的代码。 There may be a better way, but this is what I found through testing.可能有更好的方法,但这是我通过测试发现的。

#!/bin/sh
#
# ro
#

pass(){
  echo passed
  return 0;   # no errors
}
fail(){
  echo failed
  return 1;   # has an error
}
t(){
  echo true, has error
}
f(){
  echo false, no error
}

dv=$(printf "%60s"); dv=${dv// /-}

echo return code good for one use, not available for echo
echo $dv
pass
[ $? -gt 0 ] && t || f
echo "function pass: \$? $?" ' return value is gone'
echo
fail
[ $? -gt 0 ] && t || f
echo "function fail: \$? $?" ' return value is gone'
echo

echo save return code to var k for continued usage
echo $dv
pass
k=$?
[ $k -gt 0 ] && t || f
echo "function pass: \$k $k"
echo
fail
k=$?
[ $k -gt 0 ] && t || f
echo "function fail: \$k $k"
echo

# direct evaluation of the return value
# note that (...) and $(...) executes in a subshell
# with return value to calling shell
# ((...)) is for math/string evaluation

echo direct evaluations of the return value:
echo '  by if (pass) and if (fail)'
echo $dv
if (pass); then
  echo pass has no errors
else
  echo pass has errors
fi
if (fail); then
  echo fail has no errors
else
  echo fail has errors
fi

# this code results in error because of returned string (stdout)
# but comment out the echo statements in pass/fail functions and this code succeeds
echo
echo '  by if $(pass) and if $(fail) ..this succeeds if no echo to stdout from function'
echo $dv
if $(pass); then
  echo pass has no errors
else
  echo pass has errors
fi
if $(fail); then
  echo fail has no errors
else
  echo fail has errors
fi

echo
echo '  by if ((pass)) and if ((fail)) ..this always fails'
echo $dv
if ((pass)); then
  echo pass has no errors
else
  echo pass has errors
fi
if ((fail)); then
  echo fail has no errors
else
  echo fail has errors
fi

echo 
s=$(pass)
r=$?
echo pass, "s: $s  ,  r: $r"
s=$(fail)
r=$?
echo fail, "s: $s  ,  r: $r"
function assert_exit_code {
    rc=$?; if [[ $rc != 0 ]]; then echo "$1" 1>&2; fi
}

... ...

execute.sh

assert_exit_code "execute.sh has failed"

put set -o pipefail at start of any script, to return any failure将 set -o pipefail 放在任何脚本的开头,以返回任何失败

incase you do, and the test fails but the tee doesnt.如果你这样做了,并且测试失败了,但 T 恤没有。 By default $?默认情况下 $? just takes the last commands success, in this case the "tee" command只需要最后一个命令成功,在这种情况下是“tee”命令

test | tee /tmp/dump
[ $? -ne 0 ] && echo "failed"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM