gpt4 book ai didi

bash - bash if 总是返回零吗?

转载 作者:行者123 更新时间:2023-11-29 09:39:15 24 4
gpt4 key购买 nike

bash if 语句总是返回零吗?

我试过了

$ if false; then echo hi; fi;
$ echo $?

看起来像。

最佳答案

来自 help if:

if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi

Execute commands based on conditional.

The if COMMANDS list is executed. If its exit status is zero, then the then COMMANDS list is executed. Otherwise, each elif COMMANDS list is executed in turn, and if its exit status is zero, the corresponding then COMMANDS list is executed and the if command completes. Otherwise, the else COMMANDS list is executed, if present. The exit status of the entire construct is the exit status of the last command executed, or zero if no condition tested true.

Exit Status: Returns the status of the last command executed.

(强调并格式化我的)。

所以如果没有 else 部分,并且如果要测试的命令以非零代码退出,则返回代码为 0。

(但是声明 bash if statements always return zero 是错误的,如上面包含的 help if 的强调部分所述)。

关于bash - bash if 总是返回零吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47623464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com