小编ter*_*cow的帖子

sh AND 和 OR 在一个命令中

试图在一行代码中检查 3 个条件,但我被卡住了。

本质上,我需要为以下内容编码:

如果

string1 不等于 string2

string3 不等于 string4

或者

bool1 = 真

然后

显示“条件满足 - 运行代码...”。

根据评论中的要求,我更新了我的示例以尝试使问题更清晰。

#!/bin/sh

string1="a"
string2="b"
string3="c"
string4="d"
bool1=true

# the easy-to-read way ....
if [ "$string1" != "$string2" ] && [ "$string3" != "$string4" ] ; then
    echo "conditions met - running code ..."
fi

if $bool1 ; then
    echo "conditions met - running code ..."
fi

# or the shorter way ...
[ "$string1" != "$string2" ] && [ "$string3" …
Run Code Online (Sandbox Code Playgroud)

shell shell-script test

9
推荐指数
1
解决办法
4万
查看次数

标签 统计

shell ×1

shell-script ×1

test ×1