having difficulty with a simple loop - especially if statem.

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

having difficulty with a simple loop - especially if statem.

Postby siggij » Mon Mar 07, 2011 1:01 am

Having trouble with the if statement. Can anybody help please?

#!/bin/bash
while true
do
echo -n "Enter a number between 20 and 30: "
read answer
if [ "$answer" >= 20 || "$answer" <= 30 ]
then
echo "Right answer"
exit 0
else
echo "Wrong answer"
continue
fi
done
siggij
 
Posts: 5
Joined: Wed Aug 23, 2006 9:14 pm

Postby bobthebob1234 » Mon Mar 07, 2011 1:16 am

For certain you have to be lost to find the places that can't be found. Elseways, everyone would know where it was
User avatar
bobthebob1234
LXF regular
 
Posts: 1373
Joined: Thu Jan 03, 2008 9:38 pm
Location: A hole in a field

Postby nelz » Mon Mar 07, 2011 9:10 am

It's hard to suggest a solution to a problem you haven't stated. What's the script supposed to do? What is it actually doing? Have you tried it with bash -x scriptname (or added set +x to the start of the script)? And why have you got a continue statement at the end of a loop, when all it can d is continue anyway.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8553
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby siggij » Mon Mar 07, 2011 12:50 pm

I'm just trying to learn bash scripting, basically. The script is supposed to give a right answer if you enter a number between 20 and 30 including. Everyting else gives an error. Thought the code would speak for itself. I know the if statement is wrong, dont need further error detection, but set +x is a good idea anyway. I'm trying to have just one if statement something like 'if [ "$answer" -ge 20 -o -le 30 ]' but it doesnt work. They work seperatealy though. I think bash is more advanced than having a seperate if statement for every comparison. Good to know I dont need continue, I learned something new. Thanks! But the problem is not resolved.
siggij
 
Posts: 5
Joined: Wed Aug 23, 2006 9:14 pm

Postby MartyBartfast » Mon Mar 07, 2011 2:17 pm

both || and -o are logical OR, you need a logical AND, if the answer is ge 20 AND the answer is le 30 then it's OK, so the line you need is something like

Code: Select all
if [ $answer -ge 20 -a $answer -le 30 ]
Last edited by MartyBartfast on Mon Mar 07, 2011 2:27 pm, edited 2 times in total.
I have been touched by his noodly appendage.
User avatar
MartyBartfast
LXF regular
 
Posts: 819
Joined: Mon Aug 22, 2005 7:25 am
Location: Hants, UK

Postby nelz » Mon Mar 07, 2011 2:23 pm

Why don't you post the actual error? That way we can look for a solution to the problem, rather than trying to guess what the problem is.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8553
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby siggij » Mon Mar 07, 2011 8:29 pm

Thanks MartyBartfast you nailed it :) Why are so many of you irritated? Its a huge turnoff (no sex involved).

Best regards,
Siggi
Iceland
siggij
 
Posts: 5
Joined: Wed Aug 23, 2006 9:14 pm

Postby MartyBartfast » Mon Mar 07, 2011 8:33 pm

siggij wrote:Why are so many of you irritated?


Well I'd jut got back from an extended pub lunch :D, maybe everyone else had to stay in and work :wink:
I have been touched by his noodly appendage.
User avatar
MartyBartfast
LXF regular
 
Posts: 819
Joined: Mon Aug 22, 2005 7:25 am
Location: Hants, UK

Postby nelz » Mon Mar 07, 2011 10:03 pm

siggij wrote:Why are so many of you irritated?


Irritated is rather strong, after all you're only hurting yourself by asking unhelpful questions. Have you read this?
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8553
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Sorry!

Postby siggij » Mon Mar 07, 2011 10:08 pm

Im truly sorry if I was offensive, I'm just feeling vulnerable right now. Thanks for the help, guys. And thanks to nelz for the rules of engagement. I will try to follow it to my heart's content from now on.

Peace,
Siggi
siggij
 
Posts: 5
Joined: Wed Aug 23, 2006 9:14 pm

Postby nelz » Tue Mar 08, 2011 1:17 am

You weren't being offensive, you were only hurting yourself. The only person that stands to gain from others being able to answer your question is you, so it is in your best interests to give sufficient information for them to be able to do so - rather than having to make wild guesses as to what may be wrong.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8553
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK


Return to Programming

Who is online

Users browsing this forum: Yahoo [Bot] and 0 guests