一种对每个 bash 测试 && 语句执行多个语句的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11038590/
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
A way to do multiple statements per bash test && statement
提问by pn1 dude
Does anyone know of a way to execute multiple statements within a bash test? So if I use:
有谁知道在 bash 测试中执行多个语句的方法?所以如果我使用:
[[ $Var = 1 ]] && echo "yes-1" || echo "no-1"
And set Var=1
then output is: yes-1
If i set Var=2
then output is: no-1
然后设置Var=1
然后输出是:yes-1
如果我设置Var=2
然后输出是:no-1
And this work as I expected. But If i try to add another statement to execute in the mix and it doesn't work:
这正如我所料。但是,如果我尝试添加另一个语句以在混合中执行并且它不起作用:
[[ $Var = 1 ]] && echo "yes-1";echo "yes-2" || echo "no-1";echo "no-2"
Which makes sense as bash sees the command ending at; but... this is not what I want.
这是有道理的,因为 bash 看到命令结束于; 但是……这不是我想要的。
I've tried grouping and evals and functions and have had failures and successes but I'd really just like to do is have this work on one line. Anyone have any ideas?
我尝试过分组、评估和函数,并且有过失败和成功,但我真的很想做的是将这项工作放在一条线上。谁有想法?
回答by chepner
Simple command grouping should work; the syntax can be a little tricky though.
简单的命令分组应该可以工作;虽然语法可能有点棘手。
[[ $Var = 1 ]] && { echo "yes-1"; echo "yes-2"; } || { echo "no-1"; echo "no-2"; }
A few things to note:
需要注意的几点:
Heed @tvm's advice about using an
if-then-else
statement if you do anything more complicated.Everycommand inside the braces needs to be terminated with a semi-colon, even the last one.
Each brace must be separated from the surrounding text by spaces on both sides. Braces don't cause word breaks in
bash
, so "{echo" is a single word, "{ echo" is a brace followed by the word "echo".
if-then-else
如果您做任何更复杂的事情,请注意@tvm 关于使用语句的建议。大括号内的每个命令都需要以分号结尾,即使是最后一个。
每个大括号必须与周围的文本用两边的空格隔开。大括号不会导致分词
bash
,所以“{echo”是一个词,“{echo”是一个大括号,后面跟着“echo”这个词。
回答by tvm
Consider using regular IF THEN ELSE statement. Use of && and || is justified in simple test such as this:
考虑使用常规的 IF THEN ELSE 语句。&& 和 || 的使用 在这样的简单测试中是合理的:
[[ -z "$PATH" ]] && echo 'Disaster, PATH is empty!' || echo 'Everything ok!'
But, consider following command:
但是,请考虑以下命令:
true && true && true && false && true || echo 'False!'
False!
OR
或者
true && { echo true; false ; } || { echo false; true ; }
true
false
Anytime a non-zero exit status is returned, command after || is executed. As you can see, even command grouping doesn't help.
任何时候返回非零退出状态,在 || 之后执行命令 被执行。如您所见,即使是命令分组也无济于事。
Execution in subshell behaves in similar manner:
在 subshell 中执行的行为方式类似:
true && ( true; echo true; true; false ) || ( true; echo true; false )
true
true
Just use regular IF, if you need proper IF behavior.
如果您需要适当的 IF 行为,只需使用常规 IF。
回答by KurzedMetal
Use subshells:
使用子shell:
$ Var=1; [[ $Var = 1 ]] && ( echo "yes-1";echo "yes-2" ) || ( echo "no-1";echo "no-2"; )
yes-1
yes-2
$ Var=2; [[ $Var = 1 ]] && ( echo "yes-1";echo "yes-2" ) || ( echo "no-1";echo "no-2"; )
no-1
no-2