mercredi 26 février 2020

Shell : How to put multiple statements in one line if

I'm facing a little issue with my "IF" tests.

What I want to do :

if [[ "$foo" == "TEST" ]]
then
  echo "OK"
else
  echo "KO"
  return 1
fi

My first try in one line if :

[[ "$foo" == "TEST" ]] && echo "OK" || echo "KO"; return 1

But the return is applied either if the test is right or wrong.

Second try :

[[ "$foo" == "TEST" ]] && echo "OK" || (echo "KO"; return 1)

The return 1 is executed, but does not exit my function. I've found that the brackets are interpreted as a sub-shell.

I have no idea how to do what I want to do. Is that even possible ?

Thanks in advance, Steve

Aucun commentaire:

Enregistrer un commentaire