Bash script syntax error
My script is supposed to ask for a number between 1 and 10 and prints the number and the square of that number. I posted that piece of code before and got help with it but now I have to do a little error checking and stuff. The error I get is at line 20 and it says "line 20: syntax error near unexpected token `else'". Here is the script:
Code:
#!/bin/bash |
The construct for an if statement goes like this:
if [ test ]; then command else command fi Your revised script should look like this: Code:
#!/bin/bash |
A few additional changes.
First, when comparing a string use == instead of -eq Second, when checking for numbers between 1 and 10 using -le and -ge use an and statement instead of an or statement if [ $NUM -ge 1 ] || [ $NUM -le 10 ] will match any integer number (anything else will error!) if [ $NUM -ge 1 ] && [ $NUM -le 10 ] will match integer numbers between 1 and 10 only (therefore more acurate) Third, you are asking for NUM to be reentered if it is not between 1 and 10, however you are not then retesting. I have taken the liberty of changing the code by using a case statement: Code:
#!/bin/bash |
Original changes:
Code:
#!/bin/bash |
Huh, I guess I have a lot to learn, thanks everyone for your help!
|
Just spotted a typo in my original post.
The case statement for 1 to 10 should be: Code:
case $NUM in Code:
case $NUM in EDIT:- To match 1-100: Code:
case $NUM in |
All times are GMT -5. The time now is 12:00 AM. |