0

So I'm not a total Linux noob, but far from being an expert. I know some things are a built-ins and some things are not built-ins, some things are treated as literals, some other things are treated as patterns, who cares -> TLDR.

I'm having quite a lot of TLDR moments right now and I'm sick and tired of trying every possible combination or reading pages upon pages to be able to do a simple if/else. I've tried what you can see below and a bunch of other variations with [, with [[, with quotes, without quotes, etc.. I'm loosing my mind over how something so trivial in other programming languages can be so cumbersome.

All I want to do is check if a variable is either empty/unset or different from two allowed values.

if [[ -z $DEP_FLAVOUR || $DEP_FLAVOUR != "develop" || $DEP_FLAVOUR != "release" ]]; then

How can I do this so that it just works?

Thanks guys!

DanDan
  • 1,038
  • 4
  • 15
  • 28
  • I'd always work with `"$DEP_FLAVOUR"` to avoid bad effects when your variable contains whitespace or is empty. Do you know [shellcheck](https://www.shellcheck.net/)? It will help you avoid such errors. – Manfred Sep 25 '22 at 13:07
  • Thanks for the info about shellchek. Didn't know this one. But I've tried that, doesn't work – DanDan Sep 25 '22 at 13:13
  • 2
    Combining negative tests is unintuitive; you need to use `&&` instead of `||`. See ["Why does non-equality check of one variable against many values always return true?"](https://stackoverflow.com/questions/26337003/why-does-non-equality-check-of-one-variable-against-many-values-always-return-tr) (and [De Morgan's laws](https://en.wikipedia.org/wiki/De_Morgan%27s_laws) for the general logical principle at work). – Gordon Davisson Sep 25 '22 at 13:17
  • @GordonDavisson Thanks for pointing me in the right direction. I of course had a logical error in my statement, got so distracted by this bracket and quotes madness that I often trip over that I didn't notice. Changed it to `if [[ -z "$DEP_FLAVOUR" || ("$DEP_FLAVOUR" != "develop" || "$DEP_FLAVOUR" != "release") ]]; then` and now it works – DanDan Sep 25 '22 at 13:30
  • This might help: `help test | grep empty` – Cyrus Sep 25 '22 at 13:34
  • Had more than one logical error, changed it to `if [[ -n "$DEP_FLAVOUR" && "$DEP_FLAVOUR" != "develop" && "$DEP_FLAVOUR" != "release") ]]; then` in the end – DanDan Sep 25 '22 at 13:37
  • I think you need this `if [[ ! ( "$DEP_FLAVOUR" = "develop" || "$DEP_FLAVOUR" = "release" ) ]]; then` which is simpler than your last try. – Philippe Sep 25 '22 at 17:43

0 Answers0