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

Clash Royale CLAN TAG#URR8PPP
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
add a comment |
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
What does "exit theifstatement" 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 acasestatement rather than anif. 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 eitheronoroffor 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 isif [[ $var1 != 'on' && $var1 != 'off' ]]; then print_help; exit; fi
– Kes
Jan 14 at 17:17
@WilliamPursell I couldn't getcaseto work. I tried the case solution as shown below but didn't know if I could make a case ofnot!onornot!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
add a comment |
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
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
bash shell-script
edited Jan 15 at 12:30
Kes
asked Jan 14 at 13:25
KesKes
966
966
What does "exit theifstatement" 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 acasestatement rather than anif. 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 eitheronoroffor 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 isif [[ $var1 != 'on' && $var1 != 'off' ]]; then print_help; exit; fi
– Kes
Jan 14 at 17:17
@WilliamPursell I couldn't getcaseto work. I tried the case solution as shown below but didn't know if I could make a case ofnot!onornot!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
add a comment |
What does "exit theifstatement" 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 acasestatement rather than anif. 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 eitheronoroffor 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 isif [[ $var1 != 'on' && $var1 != 'off' ]]; then print_help; exit; fi
– Kes
Jan 14 at 17:17
@WilliamPursell I couldn't getcaseto work. I tried the case solution as shown below but didn't know if I could make a case ofnot!onornot!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
add a comment |
1 Answer
1
active
oldest
votes
#!/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; fiThis produces;$ var1 is NOT equal to "on" or "off". Var1 is equal to onWhat 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 ifvar1is not equal toonORvar1is not equal tooff. Sincevar1is not equal tooff, 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 inif [[ $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
|
show 10 more comments
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
#!/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; fiThis produces;$ var1 is NOT equal to "on" or "off". Var1 is equal to onWhat 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 ifvar1is not equal toonORvar1is not equal tooff. Sincevar1is not equal tooff, 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 inif [[ $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
|
show 10 more comments
#!/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; fiThis produces;$ var1 is NOT equal to "on" or "off". Var1 is equal to onWhat 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 ifvar1is not equal toonORvar1is not equal tooff. Sincevar1is not equal tooff, 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 inif [[ $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
|
show 10 more comments
#!/bin/bash
read -p "introduce a value: " var1
if [[ $var1 != 'on' && $var1 != 'off' ]];
then
exit
else
echo "CORRECT"
fi
#!/bin/bash
read -p "introduce a value: " var1
if [[ $var1 != 'on' && $var1 != 'off' ]];
then
exit
else
echo "CORRECT"
fi
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; fiThis produces;$ var1 is NOT equal to "on" or "off". Var1 is equal to onWhat 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 ifvar1is not equal toonORvar1is not equal tooff. Sincevar1is not equal tooff, 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 inif [[ $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
|
show 10 more comments
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; fiThis produces;$ var1 is NOT equal to "on" or "off". Var1 is equal to onWhat 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 ifvar1is not equal toonORvar1is not equal tooff. Sincevar1is not equal tooff, 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 inif [[ $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
|
show 10 more comments
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
What does "exit the
ifstatement" 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
casestatement rather than anif. 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
onoroffor 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 isif [[ $var1 != 'on' && $var1 != 'off' ]]; then print_help; exit; fi– Kes
Jan 14 at 17:17
@WilliamPursell I couldn't get
caseto work. I tried the case solution as shown below but didn't know if I could make a case ofnot!onornot!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