LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 12-20-2016, 07:41 AM   #1
Vonhavel
LQ Newbie
 
Registered: Dec 2016
Posts: 2

Rep: Reputation: Disabled
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.
 
Old 12-20-2016, 08:13 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
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 09:30 AM.
 
Old 12-20-2016, 08:15 AM   #3
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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 08:52 AM.
 
1 members found this post helpful.
Old 12-20-2016, 08:30 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Like minds think the same...
 
Old 12-20-2016, 08:32 AM   #5
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by michaelk View Post
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 08:44 AM.
 
Old 12-20-2016, 08:47 AM   #6
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by michaelk View Post
Like minds think the same...
sorry! Your logic is actually backward. Read post above this one.
 
Old 12-20-2016, 08:48 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
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 08:49 AM.
 
Old 12-20-2016, 08:54 AM   #8
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by michaelk View Post
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 08:56 AM.
 
Old 12-20-2016, 08:56 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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 ))
 
Old 12-20-2016, 09:05 AM   #10
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by grail View Post
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 09:22 AM.
 
Old 12-20-2016, 09:21 AM   #11
Vonhavel
LQ Newbie
 
Registered: Dec 2016
Posts: 2

Original Poster
Rep: Reputation: Disabled
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!!!
 
Old 12-20-2016, 09:28 AM   #12
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by Vonhavel View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
In a for loop that operates on multiple servers, need both the options to enter the password or to skip to next server in loop rajkamalhm Linux - Newbie 7 06-08-2016 09:28 PM
[SOLVED] if loop with multiple conditions cnitin Linux - Newbie 5 12-09-2012 11:42 PM
Bash Question(for loop): How to Zip multiple files under multiple directories Znrall Linux - General 2 08-01-2012 01:52 PM
multiple conditions in while() loop in C glosterj9 Programming 1 12-27-2011 11:16 AM
Comparing Multiple Values in while loop rahulruns Programming 5 10-27-2009 05:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration