如果 else && || 使用 bash 的正确方法是什么 捷径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15538782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:56:02  来源:igfitidea点击:

What is the correct way to use the bash if else && || shortcut?

bashif-statement

提问by ilium007

I have come across some strange behavious (well probably not strange but I don't understand it !)

我遇到了一些奇怪的行为(可能并不奇怪,但我不明白!)

I want to write some if / e,se statements using the bash shorthand syntax:

我想使用 bash 速记语法编写一些 if / e,se 语句:

[[ 1 -eq 1 ]] && echo "true" || echo "false"

The output of the above code gives:

上述代码的输出给出:

true

Now the code above actually works fine. But this next code does not:

现在上面的代码实际上工作正常。但是下一个代码不会:

[[ 1 -eq 1 ]] && infMsg "true" || infMsg "false"

infMsgis just a function. The output from the above code gives:

infMsg只是一个函数。上述代码的输出给出:

true
false

I only want it to say 'true'.

我只希望它说“真实”。

Is there something I am missing in the way the && || syntax handles exit codes from functions ?

我在 && || 的方式中遗漏了什么吗?语法处理函数的退出代码?

回答by Paul Calabro

I suspect your exit code for 'infMsg' is not 0 (success). If we break down the first piece of code:

我怀疑您的“infMsg”退出代码不是 0(成功)。如果我们分解第一段代码:

[[ 1 -eq 1 ]] && echo "true" || echo "false"

What's happening is:

发生的事情是:

  • The [[ 1 -eq 1 ]] && echo "true"portion of code is evaluated first since &&has a higher precedence than ||(more info[here1)
  • The first half of that returns true. Then, since the first half was true, the second half executes the echo "true"statement. (&& -> If part one is true, run the second part)
  • Now we have the || echo "false". This part actually doesn't get executed since the first half (echo "true") returned true since the echo printed successfully ((||-> If part one is false, run the second part).).
  • [[ 1 -eq 1 ]] && echo "true"代码的一部分第一被评估,因为&&具有比更高的优先级||(详细信息[这里1
  • 前半部分返回 true。然后,由于前半部分为 true,后半部分执行该echo "true"语句。(&& -> 如果第一部分为真,则运行第二部分)
  • 现在我们有了|| echo "false". 这部分实际上并没有被执行,因为前半部分 ( echo "true") 返回 true ,因为 echo 打印成功((||-> 如果第一部分为假,则运行第二部分。)。

Now if we break down the second piece of code:

现在,如果我们分解第二段代码:

[[ 1 -eq 1 ]] && infMsg "true" || infMsg "false"

What's most likely happeningis:

什么是最有可能发生的是:

  • [[ 1 -eq 1 ]] && infMsg "true"is evaluated. The first side is true. Then, since the first half was true, the second half executes the infMsg "true"statement. (&& -> If part one is true, run the second part).
  • Now we have || infMsg "false". If the infMsg "true"from the previous command didn't return a status code that is interpreted as success (0), the first half is interpreted as false, triggering the infMsg "false"((||-> If part one is false, run the second part).)..
  • [[ 1 -eq 1 ]] && infMsg "true"被评估。第一面是真的。然后,由于前半部分为 true,后半部分执行该infMsg "true"语句。(&& -> 如果第一部分为真,则运行第二部分)。
  • 现在我们有|| infMsg "false". 如果infMsg "true"上一个命令没有返回被解释为成功 (0) 的状态代码,则前半部分被解释为假,触发infMsg "false"( (||-> 如果第一部分为假,则运行第二部分)。)..

The important info to pull away here:

重要的信息在这里拉开:

  • || only runs the second half if the first half is FALSE.
  • && only run the second half if the first half is TRUE.
  • || 如果前半部分为 FALSE,则仅运行后半部分。
  • && 仅在前半部分为 TRUE 时才运行后半部分。