【问题标题】:What's the exit status of a failed condition?失败条件的退出状态是什么?
【发布时间】:2017-02-13 11:58:55
【问题描述】:
[ $var -eq $val ]

如果上述条件/测试失败,$? 的值将是多少?我可以假设它总是1吗?

编辑:阅读答案后,我意识到我的问题并不准确。我的意思是“如果没有发生错误,它会一直是1吗?”。

【问题讨论】:

    标签: bash conditional-statements exitstatus


    【解决方案1】:

    是的,至少只要不发生错误(我猜在一般情况下从技术上讲是“不”吗?)。见http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128_14

    应返回以下退出值:

    • 0

      表达式评估为真。

    • 1

      表达式评估为假或表达式丢失。

    • >1

      发生错误。

    https://www.gnu.org/software/bash/manual/bashref.html#Bourne-Shell-Builtins:

    计算条件表达式 expr 并返回状态 0(真)或 1(假)。

    【讨论】:

      【解决方案2】:

      没有。如果条件失败,它不会总是 1。

      例如:

      [root@localhost ~]# [ xxxxxx15 -gt "$10" ]
      bash: [: xxxxxx15: integer expression expected
      [root@localhost ~]# echo $?
      2
      

      退出状态可能因您应用的运营商/条件而异

      【讨论】:

        猜你喜欢
        • 2011-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多