Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-20-2016, 08:41 AM
|
#1
|
LQ Newbie
Registered: Dec 2016
Posts: 2
Rep: 
|
While loop multiple
Hi guys,
i have a problem with the while loop when i try add multiple conditions. I'm using the bash to make a script. This is a simply example. I tried to change the symbols and try different alternatives so that the bash grammar was correct. I have seen several post and I have tried to write the condition as explained in the post, but I do not get it  .
This is one of many examples I have seen to try to solve my problem:
http://stackoverflow.com/questions/1...-in-while-loop
Code:
#! /bin/bash
# Testing While
var=0
echo "Choose a number: 1, 2 or 3:"
read var
while [ var != 1 || var != 2 || var != 3 ]
do
echo "Choose a correct number: 1, 2 or 3:"
read var
done
case $var in
1)
echo "You choose: 1"
;;
2)
echo "You choose: 2"
;;
3)
echo "You choose: 3"
;;
esac
Thanks and sorry for my english.
|
|
|
12-20-2016, 09:13 AM
|
#2
|
Moderator
Registered: Aug 2002
Posts: 26,522
|
Welcome to LinuxQuestions.
This is a common mistake when using multiple conditions in the same if statement so let me illustrate. All conditions must be false for the if statement to be false.
Let say var=1
For the statement if (var!=1 or var!=2) = true since var!=2 is true.
Let say var=2
For the statement if (var!=1 or var!=2) = true since var!=1 is true.
Since you want all conditions to be true or false for the loop to work you need to use an and ie. &&
Now try
Let say var=1
For the statement if (var!=1 and var!=2) = false and now the script can continue.
And so...
while [[ $var != "1" && $var != "2" && $var != "3" ]]
Last edited by michaelk; 12-20-2016 at 10:30 AM.
|
|
|
12-20-2016, 09:15 AM
|
#3
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
learn variables. you wrote it wrong, then the right way.
learn comparisons.
learn http://wiki.bash-hackers.org/commands/classictest
now your example script works.
Code:
#! /bin/bash
# Testing While
set -x
var=0
echo "Choose a number: 1, 2 or 3:"
read var
while [ "${var}" -ne 3 ] && [ "${var}" -ne 2 ] && [ "${var}" -ne 1 ]
do
echo "Choose a correct number: 1, 2 or 3:"
read var
done
case $var in
1)
echo "You choose: 1"
;;
2)
echo "You choose: 2"
;;
3)
echo "You choose: 3"
;;
esac
Asking it to check to see
if one or the other one or the other one is true
is different than having it test to see
if one and the other one and the other one is true.
using ' or' only one needs one to be true to make the whole statement true, whereas using ' and' they all have to be true in order for the whole statement to be true.
It always test for true no matter what.
Code:
while [ "${var}" -ne 3 ] || [ "${var}" -ne 2 ] || [ "${var}" -ne 1 ]
Is stating that $var only one has to be a true in one test for the entire statement to be true.
Code:
while [ "${var}" -ne 3 ] && [ "${var}" -ne 2 ] && [ "${var}" -ne 1 ]
is saying that $var has to not equal to all three test in order for it to be a true statment.
MOD: looks like someone posted just before me. same O
Last edited by BW-userx; 12-20-2016 at 09:52 AM.
|
|
1 members found this post helpful.
|
12-20-2016, 09:30 AM
|
#4
|
Moderator
Registered: Aug 2002
Posts: 26,522
|
Like minds think the same...
|
|
|
12-20-2016, 09:32 AM
|
#5
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by michaelk
Welcome to LinuxQuestions.
This is a common mistake when using multiple conditions in the same if statement so let me illustrate. All conditions must be false for the if statement to be false.
(( It should read, "with 'or' only one condition must be true for the whole if statement to be true" - I want this one or that one or that one. only one condition is needed to be gained or met for the entire statement to be true))
Let say var=1
For the statement if (var!=1 or var!=2) = true since var!=2 is true.
Let say var=2
For the statement if (var!=1 or var!=2) = true since var!=1 is true.
Since statements need to be true or false for the loop to work you need to use an and ie. &&
((with 'and' all test have to be met or true for the entire statement to be true. - I need this one and that one, and that one (too). all three have to be gotten for it to be a complete true))
Now try
Let say var=1
For the statement if (var!=1 and var!=2) = false and now the script can continue.
And so...
while [[ $var != "1" && $var != "2" && $var != "3" ]]
|
not trying to be a bugger, but it is true not false. think about it.
even a != looks for a truth to be within test. if 3 != 4 then it is still a true statement. it is never a false statement, or a false test that bash or any other programming languages will act on. It is only a truth that it will act on. Just like how mankind is suppose to be. Only acting on a truth, not a false.
Last edited by BW-userx; 12-20-2016 at 09:44 AM.
|
|
|
12-20-2016, 09:47 AM
|
#6
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by michaelk
Like minds think the same...
|
sorry! Your logic is actually backward. Read post above this one.
|
|
|
12-20-2016, 09:48 AM
|
#7
|
Moderator
Registered: Aug 2002
Posts: 26,522
|
Trying to explain it from the users perspective on how the script is supposed to work but your correct an if statement runs if condition is true.
Last edited by michaelk; 12-20-2016 at 09:49 AM.
|
|
|
12-20-2016, 09:54 AM
|
#8
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by michaelk
Trying to explain it from the users perspective on how the script is supposed to work but your correct an if statement runs if condition is true.
|
and you explained it that they have to be false not true.
Quote:
All conditions must be false for the if statement to be false.
|
it is misleading. even thought that is a true statement, it looks for true, not false in order for it to actually work.
Last edited by BW-userx; 12-20-2016 at 09:56 AM.
|
|
|
12-20-2016, 09:56 AM
|
#9
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,031
|
You might want to re-read what you have criticised as I see no issue with the statement made. When using 'OR' the only time the statement is false is if ALL statements equate to false.
To the OP I would add that as you are performing arithmetic test in bash it is best to use (()) as you can then use math symbols in your tests, instead of things like '-ne'.
So your current example would be:
Code:
(( var != 1 && var != 2 && var != 3 ))
|
|
|
12-20-2016, 10:05 AM
|
#10
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by grail
You might want to re-read what you have criticised as I see no issue with the statement made. When using 'OR' the only time the statement is false is if ALL statements equate to false.
To the OP I would add that as you are performing arithmetic test in bash it is best to use (()) as you can then use math symbols in your tests, instead of things like '-ne'.
So your current example would be:
Code:
(( var != 1 && var != 2 && var != 3 ))
|
OK, Programming and life itself one is to always look for a truth within the entire statement. If it finds one true then it equates to all true because the truth it is looking for has been obtained. Therefore it will then proceed with what the next step you tell it to do. Acting on the truth that it found, not the false that is there, else it will error out, or fail because no truth was found.
your example all three have to be truth else it will not process the next step that is to be written for it to do. It will skip to the next line of code, or block of code because a false was found.
therefore that test did not meet the required truths in order for it to process the rest of the code block.
If will never act on a false it will always skip a false. Then move on looking only for truths to act on. Never false unless you use an 'else', as an alternative. It will then act on a non truth.
Code:
if [ true] then do something,
else
do something else.
Code:
if [ not equal to something] then do something
else
do something else
the test still has to be a truth for it to run the next line, else it will run the other line because it equated to a false. it is still a truth that has to be met for the test to not fail. if the truth is not met then do something else.
if someone lies to you he is false. but he still equates to a truth if you call him a lair. because you told the truth. not him. you are the [ test within the brackets ] always looking for a truth to act on it. If someone tells you it is good to drink acid and you know it is not, and you believe that person without checking for a truth then you have acted on a lie, or a false truth.
therefore if programs are written to look for and act on false truths then where would that leave you if you were to become what you write?
Last edited by BW-userx; 12-20-2016 at 10:22 AM.
|
|
|
12-20-2016, 10:21 AM
|
#11
|
LQ Newbie
Registered: Dec 2016
Posts: 2
Original Poster
Rep: 
|
SOLVED
Ok guys, i had wrong concepts about this but your help was effective. Now i know more things about conditions and next time i'll working on the truth.
THANKS!!!
|
|
|
12-20-2016, 10:28 AM
|
#12
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by Vonhavel
SOLVED
Ok guys, i had wrong concepts about this but your help was effective. Now i know more things about conditions and next time i'll working on the truth.
THANKS!!!
|
Bravo! Always work on (looking for) the truth.
|
|
|
All times are GMT -5. The time now is 07:13 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|