Linux - NewbieThis 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.
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.
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
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" ]]
#! /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.
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.
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:
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.