gpt4 book ai didi

shell - 伯恩 : if statement testing exit status

转载 作者:行者123 更新时间:2023-12-04 23:10:06 25 4
gpt4 key购买 nike

有什么不同:

if IsServerStarted ; then ...


if [ IsServerStarted -eq 0 ] ; then ...

在我看来这两个陈述应该是等价的?奇怪的是,第二个陈述总是正确的。

最佳答案

以下运行$PATH中的shell函数或可执行文件命名 IsServerStarted , 如果它的退出代码是 0 (即 true),运行 then分支。如果这样的函数或可执行文件不存在,退出代码将是非 0 (即假)和 then分支将被跳过。

if IsServerStarted ; then ...

以下有 [ (又名 test)检查是否 IsServerStarted是一个等于 0 的整数,其中( IsServerStarted 甚至不包含一个数字)总是 .因此, [以非 0 退出(即假)代码和 then总是跳过分支。
if [ IsServerStarted -eq 0 ] ; then ...

关于shell - 伯恩 : if statement testing exit status,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2699237/

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