BASH logic problem; difficulty testing one string variable with IF and OR

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












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










share|improve this question
























  • 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















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










share|improve this question
























  • 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













0












0








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










share|improve this question
















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







bash shell-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 15 at 12:30







Kes

















asked Jan 14 at 13:25









KesKes

966




966












  • 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

















  • 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
















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





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





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






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





@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 Answer
1






active

oldest

votes


















1














#!/bin/bash
read -p "introduce a value: " var1

if [[ $var1 != 'on' && $var1 != 'off' ]];
then
exit
else
echo "CORRECT"
fi





share|improve this answer

























  • 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








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494418%2fbash-logic-problem-difficulty-testing-one-string-variable-with-if-and-or%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














#!/bin/bash
read -p "introduce a value: " var1

if [[ $var1 != 'on' && $var1 != 'off' ]];
then
exit
else
echo "CORRECT"
fi





share|improve this answer

























  • 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















1














#!/bin/bash
read -p "introduce a value: " var1

if [[ $var1 != 'on' && $var1 != 'off' ]];
then
exit
else
echo "CORRECT"
fi





share|improve this answer

























  • 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













1












1








1







#!/bin/bash
read -p "introduce a value: " var1

if [[ $var1 != 'on' && $var1 != 'off' ]];
then
exit
else
echo "CORRECT"
fi





share|improve this answer















#!/bin/bash
read -p "introduce a value: " var1

if [[ $var1 != 'on' && $var1 != 'off' ]];
then
exit
else
echo "CORRECT"
fi






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 15 at 13:41









ilkkachu

57.3k786159




57.3k786159










answered Jan 14 at 13:42









DaselDasel

4497




4497












  • 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

















  • 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
















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






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




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






@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




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





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




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





@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




1





Modified the answer with only the final aswer.

– Dasel
Jan 15 at 13:36





Modified the answer with only the final aswer.

– Dasel
Jan 15 at 13:36

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494418%2fbash-logic-problem-difficulty-testing-one-string-variable-with-if-and-or%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)