bash 命令分组(&&、||、...)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29351665/
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 12:41:15  来源:igfitidea点击:

Command grouping (&&, ||, ...)

bashsyntaxoperator-precedence

提问by mythic

We are currently in the /home/student/ directory. We execute the following commands:

我们目前在 /home/student/ 目录中。我们执行以下命令:

pwd; (ls) || { cd .. && ls student/; }  && cd student || cd / && cd ;

The commands that are executed are: pwd, ls, cd student, cd /, cd

执行的命令有:pwd, ls, cd student, cd /, cd

Here is what I think:

这是我的想法:

  • pwd is executed, because it's the first command

  • (ls) is executed in a subshell, because the commands are seperated
    with ";"

  • the code on the right of || isn't executed, since the code on the
    left of || was executed
  • pwd 被执行,因为它是第一个命令

  • (ls) 在子shell中执行,因为命令是
    用“;”分隔的

  • || 右边的代码 没有执行,因为
    || 左边的代码 被处决

So far everything clear, I guess. But I have no idea why other commands are executed? If someone could break it down for me, I'd appreciate it.

到目前为止,一切都清楚了,我想。但我不知道为什么要执行其他命令?如果有人可以为我分解它,我将不胜感激。

回答by Charles Duffy

Operator precedence for &&and ||is strictly left-to-right.

运算符优先级&&||严格左到右。

Thus:

因此:

pwd; (ls) || { cd .. && ls student/; }  && cd student || cd / && cd ;

...is equivalent to...

……相当于……

pwd; { { { (ls) || { cd .. && ls student/; }; } && cd student; } || cd /; } && cd ; }

...breaking that down graphically:

...以图形方式分解:

pwd; {                                      # 1
       {                                    # 2
         { (ls) ||                          # 3
                   { cd .. &&               # 4
                              ls student/;  # 5
                   };                       # 6
         } && cd student;                   # 7
       } || cd /;                           # 8
     } && cd ;                              # 9
  1. pwdhappens unconditionally
  2. (Grouping only)
  3. lshappens (in a subshell) unconditionally.
  4. cd ..happens if (3) failed.
  5. ls student/happens if (3) failed and (4) succeeded
  6. (Grouping only)
  7. cd studenthappens if either (3) succeeded or both (4) and (5) succeeded.
  8. cd /happens if either [both (3) and one of (4) or (5) failed], or [(7) failed].
  9. cdhappens if (7) occurred and succeeded, or (8) occurred and succeeded.
  1. pwd无条件发生
  2. (仅限分组)
  3. ls无条件地(在子外壳中)发生。
  4. cd ..如果 (3) 失败,就会发生。
  5. ls student/如果 (3) 失败且 (4) 成功,则会发生
  6. (仅限分组)
  7. cd student如果 (3) 或 (4) 和 (5) 都成功,则会发生。
  8. cd /如果 [(3) 和 (4) 或 (5) 之一失败] 或 [(7) 失败] 时发生。
  9. cd如果 (7) 发生并成功,或 (8) 发生并成功,则发生。


Using explicit grouping operators is wise to avoid confusing yourself. Avoiding writing code as hard to read as this is even wiser.

使用显式分组运算符可以避免混淆。避免编写像这样难以阅读的代码更为明智。

回答by John Kugelman

(ls) is executed in a subshell, because the commands are seperated with ";"

(ls) 在子shell中执行,因为命令是用“;”分隔的

lsis executed in a subshell because of the parentheses. Parentheses introduce subshells.

ls由于括号,在子shell中执行。括号介绍子shell。

But I have no idea why other commands are executed?

但我不知道为什么要执行其他命令?

Unlike other programming languages you might be familiar with, bash does not give &&higher precedence than ||. They have equal precedence and are evaluated from left to right.

与您可能熟悉的其他编程语言不同,bash 的&&优先级不高于||. 它们具有相同的优先级并从左到右进行评估。

If you had a || b && c, in other languages this would be read as a || {b && c;}. If awere true then neither bnor cwould be evaluated.

如果有a || b && c,在其他语言中,这将被读作a || {b && c;}. 如果a为真,则既b不会也c不会被评估。

In bash, though, it's parsed as {a || b;} && c(strict left-to-right precedence), so when ais true bis skipped but cis still evaluated.

然而,在 bash 中,它被解析为{a || b;} && c(严格的从左到右的优先级),所以 when ais trueb被跳过但c仍然被评估。