check two conditions in if statement in bash
I have problem in writing if statement
var1=`COMMAND | grep <Something>`
if [ -n "$var1" ] && if [[ "$var1" != string ]]; then
.
.
.
fi
I want to write if statement that check:
If var1 is not null AND if string(could be hello
word) is not in var1 then do stuff.
How can I do that?
Just use something like this:
if [ -n "$var1" ] && [[ ! $var1 == *string* ]]; then
...
fi
See an example:
$ v="hello"
$ if [ -n "$v" ] && [[ $v == *el* ]] ; then echo "yes"; fi
yes
$ if [ -n "$v" ] && [[ ! $v == *ba* ]] ; then echo "yes"; fi
yes
The second condition is a variation of what is indicated in String contains in bash.
其他可能性:
if [ -n "$var1" -a "$var1" != string ]; then ...
if [ "${var1:-xxx}" != "string" ]; then ...
You should rephrase the condition like this:
var1=`COMMAND | grep <Something>`
if [ -n "$var1" ] && [[ "$var1" != "string" ]]; then
.
.
.
fi
or the equivalent:
if test -n "$var1" && test "$var1" != "string"
then
...
fi
链接地址: http://www.djcxy.com/p/36208.html
上一篇: 在bash脚本中获取包含下划线的文件
下一篇: 在bash中检查if语句中的两个条件