Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查$?在unix shell脚本中不等于零?_Shell_Unix - Fatal编程技术网

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

如何检查$?在unix shell脚本中不等于零?,shell,unix,Shell,Unix,我有一个脚本,它使用test命令检查$?(上次执行命令的返回代码)是否不等于零。代码如下:- $?是最后执行的命令的退出状态 if (test $? -ne 0) then //statements// fi 但是,这种验证方法不适用于字符串,因为get语法错误。请建议一个合适的替代方案 首先将其放入变量中,然后尝试测试它,如下所示 ret=$? if [ $ret -ne 0 ]; then echo "In If" else echo "In Else" f

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

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

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

但是,这种验证方法不适用于字符串,因为get语法错误。请建议一个合适的替代方案

首先将其放入变量中,然后尝试测试它,如下所示

ret=$?
if [ $ret -ne 0 ]; then
        echo "In If"
else
        echo "In Else"
fi
这应该会有所帮助



编辑:如果上述操作没有按预期进行,则可能是您没有在正确的位置使用
$?
它必须是命令后的下一行,您需要捕捉返回状态。即使在目标和您捕获其返回状态之间有任何其他单个命令,您也将检索此中间命令的返回状态,而不是您期望的状态。

我不知道您是如何在
$?
中获得字符串的,但您可以:

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

a=${?}
如果[${a}-ne 0];然后呼应“做点什么”;fi

使用您想要使用的任何命令,而不是
echo“do something”
命令这是针对类似问题提出的解决方案

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

do-command exit_status && echo "worked" || echo "didnt work"


在执行脚本后尝试以下操作:

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

您不需要测试
$?
是否不是
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
):

但是,您在问题中提供的代码可以正常工作。我对你的语法错误感到困惑&我断定
$?
是一个字符串。很可能是问题中没有提供导致语法错误的错误代码。这一点尤其明显,因为您声称其他人的解决方案也不起作用。当这种情况发生时,你必须重新评估你的假设

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

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

如果你这样做了,测试失败了,但发球台没有。默认情况下$?只需执行最后一个命令success,在本例中为“tee”命令


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

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

在您的示例中,$?实际上是数字,但由于您使用了方括号,即隐含的“test”命令,因此出现语法错误,而不是因为退出代码是字符串。谢谢…那么您建议我应该尝试什么?请参阅下面的答案,并注意方括号而不是圆括号的使用…我尝试了。。。在我的情况下不起作用!!还有其他建议吗please@user1466466-你能详细说明一下吗?您确定
$?
正在返回您期望的结果吗?试着用这个例子来确认一下,这对我很有用。我通常是乐观的,所以我做的是
[$ret-eq 0]
,但这是一个品味的问题。@user1466466我声称它应该有效。你能提供证据证明它不存在吗?换句话说,永远不要只说“它不起作用”。请告诉我们该命令做了什么,以及您希望它做什么。@user1466466请注意所有空格都很重要。例如,[和$ret之间有一个空格,以此类推。我尝试了它…它无法检查条件!!请
[…]]
在用户的shell中可能没有任何其他建议。一个更简单的选择是这样定义它:
退出状态(){test$?-eq 0;}
。在您的示例中,您可以完全取消无关的exit_status函数,并执行:
do命令和&echo“已工作”| | echo“未工作”
。或者您也可以这样做:“$?-eq 0”表示执行的某些_命令没有错误,“$?-ne 0”有错误???@happy,是的,基本正确:
$?-eq 0
表示最后一个命令退出时状态代码为
0
(成功)。
$?-ne 0
表示最后一个命令退出时状态代码为非零(通常是由于错误).我已经在类似的UC中从管道切换到if块,因为它想在出错的情况下退出脚本。()谢谢你!太好了!
if [ $? -ne 0 ];
then
//statements//
fi
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
}
some_command_returning_nonzero || {
    # executes this block of code when:     $? -ne 0
    # and nothing if the command succeeds:  $? -eq 0
}
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
test | tee /tmp/dump
[ $? -ne 0 ] && echo "failed"
#!/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"