Linux bash中if子句中的组合条件
我想在bash中这样做:Linux bash中if子句中的组合条件,linux,bash,Linux,Bash,我想在bash中这样做: //C pseudo code if(cond1 is true and (cond2 is true or cond3 is true)) do something 这就是我们所拥有的 var1=abc var2= if echo "$var1" | grep -q 'abc' && ( echo "$var2" | grep 'def' || [ "x$var2" = "x" ] ) then echo hello f
//C pseudo code
if(cond1 is true and (cond2 is true or cond3 is true))
do something
这就是我们所拥有的
var1=abc
var2=
if echo "$var1" | grep -q 'abc' && ( echo "$var2" | grep 'def' || [ "x$var2" = "x" ] )
then
echo hello
fi
这还印着你好
我真的需要使用echo grep构造。我该怎么办 假设您有Bash 3.x,您可以通过以下方式简化代码:
#!/bin/bash
var1=abc
var2=
if [[ $var1 =~ abc && ( $var2 =~ def || -z $var2 ) ]]; then
echo hello
fi
它正确地打印hello,因为当var2为空字符串时[x$var2=x]为真。不过,您可能想在第二个grep中添加-q。@abc那么为什么要将其标记为bash?