mercredi 4 juillet 2018

Nested condition of if-statement in bash

I want to check that of two variables both or neither are set. I've tried multiple options, and this is the most clean solution I've come up with:

if [ ! -z $A -a -z $B ] || [ -z $A -a ! -z $B ]; then
  #error
fi
  #success

When I run the script with both A and B set - it runs fine. But when I run it with A missing I get:

./test.sh: line 3: [: too many arguments
./test.sh: line 3: [: too many arguments

line 3 being the condition statement.

When I run it with B missing, I get:

./test.sh: line 3: [: argument expected
./test.sh: line 3: [: argument expected

Is it my condition that has wrong syntax or am I missing smth else?

Aucun commentaire:

Enregistrer un commentaire