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
Command grouping (&&, ||, ...)
提问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
pwd
happens unconditionally- (Grouping only)
ls
happens (in a subshell) unconditionally.cd ..
happens if (3) failed.ls student/
happens if (3) failed and (4) succeeded- (Grouping only)
cd student
happens if either (3) succeeded or both (4) and (5) succeeded.cd /
happens if either [both (3) and one of (4) or (5) failed], or [(7) failed].cd
happens if (7) occurred and succeeded, or (8) occurred and succeeded.
pwd
无条件发生- (仅限分组)
ls
无条件地(在子外壳中)发生。cd ..
如果 (3) 失败,就会发生。ls student/
如果 (3) 失败且 (4) 成功,则会发生- (仅限分组)
cd student
如果 (3) 或 (4) 和 (5) 都成功,则会发生。cd /
如果 [(3) 和 (4) 或 (5) 之一失败] 或 [(7) 失败] 时发生。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中执行,因为命令是用“;”分隔的
ls
is 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 a
were true then neither b
nor c
would 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 a
is true b
is skipped but c
is still evaluated.
然而,在 bash 中,它被解析为{a || b;} && c
(严格的从左到右的优先级),所以 when a
is trueb
被跳过但c
仍然被评估。