繁体   English   中英

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

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

我有一个脚本,它使用 test 命令来检查$? (上次执行命令的返回码)不为零。 代码如下:-

$? 是最后执行的命令的退出状态。

if (test $? -ne 0)
then
//statements//
fi

然而,这种验证方式不适用于字符串,因为 get syntax error 。 请提出一个合适的替代方案。

先把它放在一个变量中,然后尝试测试一下,如下图

ret=$?
if [ $ret -ne 0 ]; then
        echo "In If"
else
        echo "In Else"
fi

这应该有帮助。


编辑:如果上述内容没有按预期工作,那么您可能没有使用$? 在正确的地方。 它必须是您需要捕获返回状态的命令之后的下一行 即使目标之间有任何其他单个命令并且您捕获它的返回状态,您也将检索此中间命令的returns_status,而不是您期望的那个。

你不需要测试是否$? 不是0 shell 提供&&|| 因此您可以根据该测试的隐式结果轻松进行分支:

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
}

您可以根据需要删除任一分支。 因此,如果您只想测试失败(即$? -ne 0 ):

some_command_returning_nonzero || {
    # executes this block of code when:     $? -ne 0
    # and nothing if the command succeeds:  $? -eq 0
}

但是,您在问题中提供的代码按原样有效。 我很困惑你有语法错误并得出结论$? 是一个字符串。 问题很可能没有提供导致语法错误的错误代码。 这一点尤其明显,因为您声称其他人的解决方案也不起作用。 发生这种情况时,您必须重新评估您的假设。

注意:如果大括号内的代码返回错误,上面的代码可能会给出令人困惑的结果。 在这种情况下,只需使用 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

执行脚本后试试这个:

if [ $? -ne 0 ];
then
//statements//
fi

我不知道你是如何得到$?的字符串的$? 但你可以这样做:

if [[ "x$?" == "x0" ]]; then
   echo good
fi

这是针对类似问题提出的解决方案

exit_status () {
if [ $? = 0 ]
then
    true
else
    false
fi
}

用法:

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

使用您想使用的任何命令,而不是echo "do something"命令

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

我整理了一些可能有助于了解返回值与返回字符串如何工作的代码。 可能有更好的方法,但这是我通过测试发现的。

#!/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"

将 set -o pipefail 放在任何脚本的开头,以返回任何失败

如果你这样做了,并且测试失败了,但 T 恤没有。 默认情况下 $? 只需要最后一个命令成功,在这种情况下是“tee”命令

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

暂无
暂无

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

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