Bash 测试中的复合条件组

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

Groups of compound conditions in Bash test

bashif-statement

提问by d3pd

I want to have some groups of conditions in a Bash if statement. Specifically, I'm looking for something like the following:

我想在 Bash if 语句中包含一些条件组。具体来说,我正在寻找如下内容:

if <myCondition1 and myCondition2> or <myCondition3 and myCondition4> then...

How may I group the conditions together in the way I describe for use with one if statement in Bash? I thank you for any ideas you may have on this.

我如何按照我描述的方式将条件组合在一起,以便与 Bash 中的一个 if 语句一起使用?我感谢您对此的任何想法。

回答by William

Use the && (and) and || (or) operators:

使用 && (and) 和 || (或)运营商:

if [[ expression ]] && [[ expression ]] || [[ expression ]] ; then

They can also be used within a single [[ ]]:

它们也可以在单个 [[ ]] 中使用:

if [[ expression && expression || expression ]] ; then

And, finally, you can group them to ensure order of evaluation:

最后,您可以将它们分组以确保评估顺序:

if [[ expression && ( expression || expression ) ]] ; then