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.
Other possibilities:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With