bash - Bash 测试中的复合条件组

标签 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/

相关文章:

regex - 如何用sed插入html标签?

javascript - 在 native react 中,当我在渲染部分放置 if 语句检查变量的值时,出现错误

javascript - 是否可以在普通 Javascript 中同时运行多个 if 语句?如果是这样你怎么办?

C# - 不应该的 CS0136

javascript - 简单的 Javascript 我不知道如何将函数放在 if-else 语句中

xml - 用包含斜杠的字符串替换 xml 属性

bash - shell 脚本 (macOS) : print escaped Unicode character from a plist string

linux - 为 Linux bash 或 shell 脚本提供自动完成和错误检测的 IDE?

macos - 递归查找所有匹配特定模式的文件

python - 如果 key 在 Python 中包含子字符串