0

I have a string var1 which I am using in BASH. It can be three values

var1=on

var1=off

or var1 can be equal to anything that is not on or off

I wish to build an if statement in BASH that tests $var1 for its content as follows;

If the $var1 string equals "on" or "off" then exit the if statement. if $var1 equals anything else continue on ...

I have tried loads of possibilities including -ne, !=, || and -o and am in a complete muddle.

Below I've pasted what I am using, what I am getting using this, and what I want.

var1=on

if [[ ${var1} != 'on' || $var1 != 'off' ]]; then echo var1 is NOT equal to \"on\" or \"off\". Var1 is equal to $var1 else echo var1 IS equal to \"on\". Var1 is equal to $var1 fi

This produces;

var1 is NOT equal to "on" or "off". Var1 is equal to on

What I want is;

var1 IS equal to "on". Var1 is equal to on

  • What does "exit the if statement" mean? "Continuing on" as the alternative sounds very similar to "exiting the if statement". Perhaps some pseudocode would illustrate your situation more clearly? – Jeff Schaller Jan 14 at 15:11
  • Sounds very much like you want a case statement rather than an if. Perhaps by "continue on" you mean that there is a loop here somewhere? – William Pursell Jan 14 at 16:46
  • Hi @WilliamPursell Thanks. There's no loop. It's either on or off or anything else. Three options only. If it's anything else a help file is printed. The solution was from @fra-san in comments below and is if [[ ${var1} != 'on' && $var1 != 'off' ]]; then print_help; exit; fi – Kes Jan 14 at 17:17
  • @WilliamPursell I couldn't get case to work. I tried the case solution as shown below but didn't know if I could make a case of not ! on or not ! off. It has to be the inverse to be neat and I couldn't establish if that could be done. Thanks – Kes Jan 14 at 17:28
1
#!/bin/bash
read -p "introduce a value: " var1

if [[ ${var1} != 'on' && ${var1} != 'off' ]];
then
        exit
else
        echo "CORRECT"
fi
  • var1=on; if [[ ${var1} != 'on' || $var1 != 'off' ]]; then echo var1 is NOT equal to \"on\" or \"off\". Var1 is equal to $var1; else echo var1 IS equal to \"on\". Var1 is equal to $var1; fi This produces; $ var1 is NOT equal to "on" or "off". Var1 is equal to on What I'm expecting is; $ var1 IS equal to "on". Var1 is equal to on – Kes Jan 14 at 14:19
  • 1
    @Kes. Here you are testing if var1 is not equal to on OR var1 is not equal to off. Since var1 is not equal to off, the result is true and the corresponding line is printed. To test for non-equality you have to replace the || (OR) operator with && (AND), as in if [[ ${var1} != 'on' && $var1 != 'off' ]]; then .... – fra-san Jan 14 at 14:33
  • 1
    I misunderstood, @Kes. Dasel, would you be willing to make the updates that fra-san suggested in this comment thread, in order to answer Dasel's question? – Jeff Schaller Jan 14 at 15:25
  • 1
    @Kes For future use: you should probably have edited your question, adding the content of the first comment you left on this answer. Comments can be deleted, and this answer could end up looking a bit illogical without them. – fra-san Jan 14 at 17:45
  • 1
    Modified the answer with only the final aswer. – Dasel Jan 15 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.