| View previous topic :: View next topic |
| Author |
Message |
siggij
Joined: Wed Aug 23, 2006 10:14 pm Posts: 5
|
Posted: Mon Mar 07, 2011 1:01 am Post subject: having difficulty with a simple loop - especially if statem. |
|
|
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 |
|
 |
bobthebob1234 LXF regular

Joined: Thu Jan 03, 2008 9:38 pm Posts: 1356 Location: A hole in a field
|
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8000 Location: Warrington, UK
|
Posted: Mon Mar 07, 2011 9:10 am Post subject: |
|
|
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. _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
siggij
Joined: Wed Aug 23, 2006 10:14 pm Posts: 5
|
Posted: Mon Mar 07, 2011 12:50 pm Post subject: |
|
|
| 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 |
|
 |
MartyBartfast LXF regular

Joined: Mon Aug 22, 2005 8:25 am Posts: 780 Location: Hants, UK
|
Posted: Mon Mar 07, 2011 2:17 pm Post subject: |
|
|
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 |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8000 Location: Warrington, UK
|
Posted: Mon Mar 07, 2011 2:23 pm Post subject: |
|
|
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. _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
siggij
Joined: Wed Aug 23, 2006 10:14 pm Posts: 5
|
Posted: Mon Mar 07, 2011 8:29 pm Post subject: |
|
|
Thanks MartyBartfast you nailed it Why are so many of you irritated? Its a huge turnoff (no sex involved).
Best regards,
Siggi
Iceland |
|
| Back to top |
|
 |
MartyBartfast LXF regular

Joined: Mon Aug 22, 2005 8:25 am Posts: 780 Location: Hants, UK
|
Posted: Mon Mar 07, 2011 8:33 pm Post subject: |
|
|
| siggij wrote: | | Why are so many of you irritated? |
Well I'd jut got back from an extended pub lunch , maybe everyone else had to stay in and work  _________________ I have been touched by his noodly appendage. |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8000 Location: Warrington, UK
|
Posted: Mon Mar 07, 2011 10:03 pm Post subject: |
|
|
| 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? _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
siggij
Joined: Wed Aug 23, 2006 10:14 pm Posts: 5
|
Posted: Mon Mar 07, 2011 10:08 pm Post subject: Sorry! |
|
|
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 |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8000 Location: Warrington, UK
|
Posted: Tue Mar 08, 2011 1:17 am Post subject: |
|
|
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. _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|