LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script syntax error (https://www.linuxquestions.org/questions/programming-9/bash-script-syntax-error-599894/)

snowman81 11-15-2007 01:17 PM

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

action()
{
    echo "your number is  $NUM"
    FINAL=$(( $NUM * NUM ))
    echo "The square of your number is  $FINAL"
}


#Main method

echo "Please choose a number between 1 and 10"
read NUM

if [ "$NUM" -lt 1 ] || [ "$NUM" -gt 10 ] then
        echo "Please choose a number between 1 and 10"
read NUM

else
        if [ "$NUM" -ge 1 ] || [ "$NUM" -le 10 ] then
action

else
        if [ "$NUM" -eq "exit" ] then

exit
fi


wolfperkins 11-15-2007 01:24 PM

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

action()
{
    echo "your number is  $NUM"
    FINAL=$(( $NUM * NUM ))
    echo "The square of your number is  $FINAL"
}


#Main method

echo "Please choose a number between 1 and 10"
read NUM

if [ "$NUM" -lt 1 ] || [ "$NUM" -gt 10 ]; then
        echo "Please choose a number between 1 and 10"
        read NUM
elif [ "$NUM" -ge 1 ] || [ "$NUM" -le 10 ]; then
        action
elif [ "$NUM" -eq "exit" ]; then
        exit
fi


Disillusionist 11-15-2007 03:06 PM

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

action()
{
    echo "Your number is  $NUM"
    FINAL=$(( $NUM * NUM ))
    echo "The square of your number is  $FINAL"
}


while true
do
  echo "Please choose an integer number between 1 and 10"
  read NUM

  case $NUM in
      [123456789]|[10] ) action
                        exit;;

      [Ee][Xx][Ii][Tt] ) exit;;

      * ) echo "$NUM is not an integer number between 1 and 10";;
  esac
done


Disillusionist 11-15-2007 03:11 PM

Original changes:

Code:

#!/bin/bash

action()
{
    echo "Your number is  $NUM"
    FINAL=$(( $NUM * NUM ))
    echo "The square of your number is  $FINAL"
}


#Main method

echo "Please choose a number between 1 and 10"
read NUM

while true
do
  if [ "$NUM" == "exit" ]; then
        exit
  elif [ "$NUM" -lt 1 ] || [ "$NUM" -gt 10 ]; then
        echo "Please choose a number between 1 and 10"
        read NUM
  elif [ "$NUM" -ge 1 ] && [ "$NUM" -le 10 ]; then
        action
        exit
  fi
done

The case statement (see previous post), resolves issues that would occur if someone tried typing "Oh I don't know" instead of "3"

snowman81 11-15-2007 05:39 PM

Huh, I guess I have a lot to learn, thanks everyone for your help!

Disillusionist 11-16-2007 02:35 AM

Just spotted a typo in my original post.

The case statement for 1 to 10 should be:
Code:

  case $NUM in
      [123456789]|10 ) action
                      exit;;

      [Ee][Xx][Ii][Tt] ) exit;;

      * ) echo "$NUM is not an integer number between 1 and 10";;
  esac

This can be simplified to:
Code:

  case $NUM in
      [1-9]|10 ) action
                exit;;

      [Ee][Xx][Ii][Tt] ) exit;;

      * ) echo "$NUM is not an integer number between 1 and 10";;
  esac

Note there should be no square brackets around 10, otherwise it will search for either 0 or 1 but not match 10.

EDIT:-

To match 1-100:

Code:

  case $NUM in
      [1-9]|[1-9][0-9]|100 ) action
                exit;;

      [Ee][Xx][Ii][Tt] ) exit;;

      * ) echo "$NUM is not an integer number between 1 and 100";;
  esac

:D


All times are GMT -5. The time now is 12:00 AM.