简体   繁体   English

为什么shell中的“false -a true”和“true -a false”返回不同的结果?

[英]Why ”false -a true“ and "true -a false" in shell return a different results?

IMO, -a and && are all and operation in shell, so I assume true -a false and false -a true would both return "false". IMO, -a&&都是 shell 中and操作,所以我假设true -a falsefalse -a true都会返回“false”。 Well the fact is那么事实是

[root@master ~] true -a false
[root@master ~] echo $?
0
[root@master ~] false -a true
[root@master ~] echo $?
1
[root@master ~] a=[ true -a false ]
[root@master ~] echo $a
true
[root@master ~] a=[ false -a true ]
[root@master ~] echo $a
true

Why did this happen, and what should I do if I want to operate and in Shell?为什么会发生这种情况,如果我想在壳牌中操作and我应该怎么做?

Let's the general syntax of a command in shell is:让我们在 shell 中的命令的一般语法是:

[var=val...] cmd [args...]

The var=val are exporting environment variable var with the value val for the duration of command cmd . var=val在命令cmd的持续时间内使用值val导出环境变量var

Why did this happen为什么会发生这种情况

Both commands true and false ignore arguments.命令truefalse都忽略参数。 true anything exits with 0 exit status, false anything exits with non-zero exit status. true anything以 0 退出状态退出, false anything以非零退出状态退出。 true -a false executes command true with two arguments, sting -a and string false . true -a false使用两个参数执行命令true , sting -a和 string false true ignores arguments, exits with zero exit status. true忽略参数,以零退出状态退出。 The same for false .相同的false

a=[ exports a variable a with the value of string [ to the environment of the next command. a=[将值为字符串[的变量a导出到下一个命令的环境。

$ a=[ env | grep '^a='
a=[

a=[ true -a false ] set's environment variable a to the value of string [ and then executes command true with 3 arguments string -a , the string false and the string ] . a=[ true -a false ]将环境变量a设置为字符串[的值,然后使用 3 个参数 string -a 、字符串false和字符串]执行命令true

  a=[ true -a false ]
  |   |    |  |     \----  third argument
  |   |    |  \----------  second argument
  |   |    \-------------  first argument
  |   \------------------  command to execute
  \----------------------  var=val, exports a variable

true ignores the arguments and true once again, exits with exit status 0 . true忽略参数,再次为true ,退出状态0 The same is for false , with non-zero exit status. false也是如此,退出状态非零。

The value echo $a that shows true is the value that you previously have set in your shell.显示true的值echo $a是您之前在 shell 中设置的值。 The a=[ true ... only set's the variable a for the duraction of the command, after that the variable has it's own value. a=[ true ...仅在命令的持续时间内设置变量a ,之后变量具有自己的值。

$ a=anything_here
$ a=anything_here2 true anything_here3
$ echo $?
0               # true exits with 0 exit status
$ echo $a
anything_here   # the variable `a` preserved its value set before the command

Why ”false -a true“ and "true -a false" in shell return a different results?为什么shell中的“false -a true”和“true -a false”返回不同的结果?

Because commands false and true exit with different exit status.因为命令falsetrue以不同的退出状态退出。

Note: the strings true false [ ] -a have no special meaning for shell.注意:字符串true false [ ] -a对于 shell 没有特殊含义。 These are all strings.这些都是字符串。 The string -a and ] have special meaning when passed as an argument for the command [ .当作为命令[的参数传递时,字符串-a]具有特殊含义。 It's still the string -a , but when the executable [ is executed, it parses the arguments and acts specially on the string -a .它仍然是字符串-a ,但是当可执行文件[被执行时,它会解析参数并专门作用于字符串-a There happen to be executables named true and false .恰好有名为truefalse的可执行文件。 The strings [ , ] , true , false or -a by itself have no significant for shell, these are all just strings.字符串[ , ]truefalse-a本身对 shell 没有意义,这些都只是字符串。

-a and && are all and operation in shell, -a 和 && 都是和 shell 中的操作,

No, -a is the string -a in shell.不, -a是 shell 中的字符串-a && is an "and" operation in shell. &&是 shell 中的“与”操作。 -a is the "and" operator for [ or test commands . -a[test commands的“与”运算符。 Note: because of problems with [ , prefer to use [ ... ] && [ ... ] instead of [ ... -a ... ] .注意:由于[的问题,更喜欢使用[ ... ] && [ ... ]而不是[ ... -a ... ]

References: https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/test.html , https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Simple-Command-Expansion , https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/true.html , https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/false.html , https://pubs.opengroup.org/onlinepubs/009604499/utilities/xcu_chap02.html#tag_02_09_01 .参考: https ://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/test.html,https: //www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Simple-Command -扩展, https : //pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/true.html,https : //pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/false.html,https ://pubs .opengroup.org/onlinepubs/009604499/utilities/xcu_chap02.html#tag_02_09_01

Thanks for KamilCuk's answer.感谢 KamilCuk 的回答。 After reading that ,here's my shorter version.读完之后,这是我的简短版本。

true -a false and false -a true is just a command who contains two arguments( -a [false/true]). true -a falsefalse -a true只是一个包含两个参数的命令(-a [false/true])。 Its exit code depend on the command itself which return 0 and 1 respectively.它的退出代码取决于分别返回 0 和 1 的命令本身。

a=[ true -a false is the same but variable a is being set to '[' before execute true -a false . a=[ true -a false相同,但变量a在执行true -a false之前被设置为'['

a shall be unset if not set -a or it's will be '['如果未设置a则应取消set -a否则它将是'['

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM