Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

having difficulty with a simple loop - especially if statem.

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
siggij



Joined: Wed Aug 23, 2006 10:14 pm
Posts: 5

PostPosted: Mon Mar 07, 2011 1:01 am    Post subject: having difficulty with a simple loop - especially if statem. Reply with quote

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
Back to top
View user's profile Send private message
bobthebob1234
LXF regular


Joined: Thu Jan 03, 2008 9:38 pm
Posts: 1369
Location: A hole in a field

PostPosted: Mon Mar 07, 2011 1:16 am    Post subject: Reply with quote

No expert with bash but :
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

and

http://www.linuxconfig.org/Bash_scripting_Tutorial

would suggest you want -le and -ge
_________________
For certain you have to be lost to find the places that can't be found. Elseways, everyone would know where it was
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8456
Location: Warrington, UK

PostPosted: Mon Mar 07, 2011 9:10 am    Post subject: Reply with quote

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)
Back to top
View user's profile Send private message
siggij



Joined: Wed Aug 23, 2006 10:14 pm
Posts: 5

PostPosted: Mon Mar 07, 2011 12:50 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
MartyBartfast
LXF regular


Joined: Mon Aug 22, 2005 8:25 am
Posts: 814
Location: Hants, UK

PostPosted: Mon Mar 07, 2011 2:17 pm    Post subject: Reply with quote

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:

if [ $answer -ge 20 -a $answer -le 30 ]

_________________
I have been touched by his noodly appendage.


Last edited by MartyBartfast on Mon Mar 07, 2011 2:27 pm; edited 2 times in total
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8456
Location: Warrington, UK

PostPosted: Mon Mar 07, 2011 2:23 pm    Post subject: Reply with quote

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)
Back to top
View user's profile Send private message
siggij



Joined: Wed Aug 23, 2006 10:14 pm
Posts: 5

PostPosted: Mon Mar 07, 2011 8:29 pm    Post subject: Reply with quote

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

Best regards,
Siggi
Iceland
Back to top
View user's profile Send private message
MartyBartfast
LXF regular


Joined: Mon Aug 22, 2005 8:25 am
Posts: 814
Location: Hants, UK

PostPosted: Mon Mar 07, 2011 8:33 pm    Post subject: Reply with quote

siggij wrote:
Why are so many of you irritated?


Well I'd jut got back from an extended pub lunch Very Happy, maybe everyone else had to stay in and work Wink
_________________
I have been touched by his noodly appendage.
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8456
Location: Warrington, UK

PostPosted: Mon Mar 07, 2011 10:03 pm    Post subject: Reply with quote

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)
Back to top
View user's profile Send private message
siggij



Joined: Wed Aug 23, 2006 10:14 pm
Posts: 5

PostPosted: Mon Mar 07, 2011 10:08 pm    Post subject: Sorry! Reply with quote

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
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8456
Location: Warrington, UK

PostPosted: Tue Mar 08, 2011 1:17 am    Post subject: Reply with quote

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)
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast