jeudi 9 mai 2019

if [-z $var1 ] && [$var2 == "1" ]; then not working as expected [duplicate]

This question already has an answer here:

I'm writing a bash script where i need to combine two conditions with && operator

var1=value
var2=1

if [-z $var1 ] && [$var2=="1"]; then
   do something
else
   do something else
fi

but it always executes the else part.

My research

  1. google gave me bash conditions like this but its not working for me.
if [condition1] && [condition2]; then
 do something
fi

  1. Another method i tried is this but it still completely ignored the true part
if [[-z $var1 ]] && [$var2=="1"]; then
   do something
else
   do something else
fi

  1. Tried with -a operator like this
if [-z $var1  -a $var2=="1" ]; then

  1. tried nested if
if [-z $var1 ]; then
    if [$var2=="1"]; then
      do something
    fi
else
      do something else
fi

So i know i am doing something wrong but my goal is i want to check for any value in $var1 and also want $var2 condition to be true and execute the true part.

Aucun commentaire:

Enregistrer un commentaire