1

I'd like to compare two strings and then another two strings. So if $version is not equal to 5 and type is not equal to dbma OR if $version is not equal to 6 and $type is not equal to dbmy

I can't quite get the syntax right.

if [[ "${version}" != "5" ]] &&  [[ "${type}" != "dbma" ]] || [[ "${version}" != "6" ]] &&  [[ "${type}" != "dbmy" ]]
then
   xyz
else
   abc
fi

Can someone please help?

1
  • 2
    Your question is ambiguous just like your code. The shell parses this left to right so you have effectively if ((version is not 5 and type is not "dbma") or version is not 6) and type is not "dbmy" ... but you should add similar brackets to your question to specify what you mean. I'm guessing you want (foo and bar) or (baz and quux) actually? Commented Sep 11, 2015 at 9:06

1 Answer 1

1

Assuming this is what you actually mean ...

if [[ ($version != 5 && $type != "dbma") || ($version != 6 && type != "dbmy") ]]

The [[ built-in allows parentheses and logical operators within the expression (whereas legacy [ had neither, and newer versions have -a and -o for "and" and "or", but no parentheses).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.