<分区>
我想在 Bash if 语句中包含一些条件组。具体来说,我正在寻找类似以下内容的内容:
if <myCondition1 and myCondition2> or <myCondition3 and myCondition4> then...
我如何按照我描述的方式将条件组合在一起,以便在 Bash 中使用一个 if 语句?
标签 bash if-statement
<分区>
我想在 Bash if 语句中包含一些条件组。具体来说,我正在寻找类似以下内容的内容:
if <myCondition1 and myCondition2> or <myCondition3 and myCondition4> then...
我如何按照我描述的方式将条件组合在一起,以便在 Bash 中使用一个 if 语句?
最佳答案
使用&&(与)和|| (或)运营商:
if [[ expression ]] && [[ expression ]] || [[ expression ]] ; then
它们也可以在单个 [[ ]] 中使用:
if [[ expression && expression || expression ]] ; then
最后,您可以将它们分组以确保评估顺序:
if [[ expression && ( expression || expression ) ]] ; then
关于bash - Bash 测试中的复合条件组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14964805/