Results 1 to 5 of 5
Hi everybody, my first post here.
I have just recently picked up my first linux distro. I dual boot ubuntu and windows xp until i can use ubuntu without help. ...
- 04-20-2011 #1Just Joined!
- Join Date
- Apr 2011
- Posts
- 3
help with else if scripting
Hi everybody, my first post here.
I have just recently picked up my first linux distro. I dual boot ubuntu and windows xp until i can use ubuntu without help. I like it so far and what I really like is that my cell phone which is a palm pre runs linux as well. I can ssh into it and control it from my ubuntu console.
Anyway I have learned quite a bit just from google but I am stuck right now and google is not really yielding the results i seek.
So my problem is I am working on a really simple script that has no value except that I want to learn these commands and I keep getting the "bad variable" error.
I know what this means but I dont know how to fix it. I am trying to create this script that when run you will pick a number 1-10 and try to guess the predetermined result, in this case the number is 7. I realize that there are many errors so if anybody could point me in the right direction that would be great.
why is "number" a bad variable. Thanks in advance for any help. Also please dont flame me too much for sloppy work because i dont learn by being flamed i can only learn if know what i did wrong.Code:echo "Pick a number 1 through 10:" read number if $number -ge 8 then echo "wrong number" else if $number -le 6 then echo "wrong number" else if $number -eq 7 then echo "correct number" fi
Last edited by noob1; 04-20-2011 at 09:48 PM.
- 04-20-2011 #2Linux Guru
- Join Date
- Nov 2007
- Posts
- 1,695
Bash Scripting Guide
Tests
Also, please try and encapsulate your "code" entries on the forum using the CODE tag around that text.
Example code text:
Code:CODE...HERE
- 04-20-2011 #3Just Joined!
- Join Date
- Apr 2011
- Posts
- 3
thanks for the quick reply. I read over the pages you listed however they are a little out of my league right now. I can only understand the simple stuff.
so is there any simple way to make numbers 0-9 a variable? i figured numbers would have been built in variables.
- 04-20-2011 #4Linux Guru
- Join Date
- Nov 2007
- Posts
- 1,695
#1 List the exact steps taken and the exact error messages verbatim when reporting an issue.
#2 Review the Bash guide again. You don't have a valid if-then stanza - your syntax is wrong. When your syntax is wrong, errors spit out by the interpreter may not be the actual issue. Google "bash debugging" - put #!/bin/bash -x at the beginning of your script to echo some debug info.
7.3. Other Comparison Operators
A binary comparison operator compares two variables or quantities. Note that integer and string comparison use a different set of operators.
integer comparison
-eq
is equal to
if [ "$a" -eq "$b" ]
-ne
is not equal to
if [ "$a" -ne "$b" ]
-gt
is greater than
if [ "$a" -gt "$b" ]
-ge
is greater than or equal to
if [ "$a" -ge "$b" ]
-lt
is less than
if [ "$a" -lt "$b" ]
-le
is less than or equal to
if [ "$a" -le "$b" ]
Else if and elif
elif
elif is a contraction for else if. The effect is to nest an inner if/then construct within an outer one.
if [ condition1 ]
then
command1
command2
command3
elif [ condition2 ]
# Same as else if
then
command4
command5
else
default-command
fi
- 04-20-2011 #5Just Joined!
- Join Date
- Apr 2011
- Posts
- 3
Thank you again. That has given me plenty to digest and work with. Thank you so much. I am happy to be a part of the community.


Reply With Quote
