LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-15-2007, 02:17 PM   #1
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 225

Rep: Reputation: 30
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
 
Old 11-15-2007, 02:24 PM   #2
wolfperkins
Member
 
Registered: Oct 2007
Location: Val-des-Monts, Québec, Canada
Distribution: CentOS, RHEL, Fedora
Posts: 109

Rep: Reputation: 16
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
 
Old 11-15-2007, 04:06 PM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
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
 
Old 11-15-2007, 04:11 PM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
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"

Last edited by Disillusionist; 11-15-2007 at 04:18 PM.
 
Old 11-15-2007, 06:39 PM   #5
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 225

Original Poster
Rep: Reputation: 30
Huh, I guess I have a lot to learn, thanks everyone for your help!
 
Old 11-16-2007, 03:35 AM   #6
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
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

Last edited by Disillusionist; 11-16-2007 at 03:39 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check the bash script syntax? mesh2005 Linux - General 2 04-23-2006 09:22 AM
bash syntax when envoking tar command in python script tangle Programming 4 02-04-2006 04:22 PM
BASH : syntax error semaja2 Programming 6 01-26-2006 01:33 AM
BASH - simple script issue - syntax? tw1ggy5 Programming 11 05-21-2004 10:09 AM
help with basic syntax in bash script Supp0rtLinux Linux - Software 4 03-27-2003 07:57 PM


All times are GMT -5. The time now is 01:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration