LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Simple bash script "unexpected end of line error" (https://www.linuxquestions.org/questions/programming-9/simple-bash-script-unexpected-end-of-line-error-597907/)

snowman81 11-07-2007 07:15 PM

Simple bash script "unexpected end of line error"
 
I'm kind of new to scripting so I have created a simple script that utilizes a sub function to read in a number and echos out the number and the square of that number. However, I get this error " line 18: syntax error: unexpected end of file"

Here is the code:
Code:

#!/bin/bash


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


#Main method

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


I know it's a simple error but I've been looking for awhile and have no idea what's wrong.

cfaj 11-07-2007 07:32 PM

Quote:

Originally Posted by snowman81 (Post 2951573)
I'm kind of new to scripting so I have created a simple script that utilizes a sub function to read in a number and echos out the number and the square of that number. However, I get this error " line 18: syntax error: unexpected end of file"

Here is the code:
Code:

#!/bin/bash


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


#Main method

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


I know it's a simple error but I've been looking for awhile and have no idea what's wrong.


Do not use parenthese when you call the function:
Code:

read NUM
action


chrism01 11-07-2007 07:38 PM

Also how about

$NUM * $NUM = $FINAL ???

did you mean

$FINAL = $NUM * $NUM

snowman81 11-07-2007 07:52 PM

I made both changes and I have a new error. Taking out the parentheses fixed the one but when I changed
Code:

$NUM * $NUM = $FINAL
to
Code:

$FINAL = $NUM * $NUM
it says "line 7: =: command not found"

matthewg42 11-07-2007 07:56 PM

Try this:

Code:

#!/bin/bash

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


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


snowman81 11-07-2007 08:07 PM

Excellent, it works. Thank you. Could you explain why it works though? I mean, what exactly the parentheses did and everything?

cfaj 11-07-2007 08:47 PM

Quote:

Originally Posted by snowman81 (Post 2951601)
I made both changes and I have a new error. Taking out the parentheses fixed the one but when I changed
Code:

$NUM * $NUM = $FINAL
to
Code:

$FINAL = $NUM * $NUM
it says "line 7: =: command not found"


Code:

FINAL=$(( $NUM * $NUM ))

Hobbletoe 11-08-2007 06:50 AM

Well, if you'll notice, matthewg42 and cfaj actually did two things to correct that line. The one that you noticed was the use of the $(( )) construct which just tells bash that you are doing some math. The second thing was to remove the spaces around the equal sign. In bash you can't have any spaces around that equal sign, or it will fail.

Code:

# The following line will work, no spaces
FINAL=$(( $NUM * $NUM ))

# The following line will not work because of the spaces
FINAL = $(( $NUM * $NUM ))

Check out the Advanced Bash-Scripting Guide. Section 4.2 would cover the assignment, and section 9.7 covers the $(( )) construct.

colucix 11-08-2007 07:48 AM

I'd add that to assign a value to a variable, the $ sign must not precede the variable name, that is
Code:

FINAL=$(( $NUM * $NUM ))
when the shell encounters the $SOMETHING syntax, it substitutes the value of the variable SOMETHING and then execute the command in which it appears

snowman81 11-08-2007 09:09 AM

Ahhh, ok, got it. Thanks guys.

SiegeX 11-11-2007 05:42 AM

Minor tip, you can actually remove the '$' in front of the variables when its inside the $(( )) construct and just do:

Code:

FINAL=$((NUM * NUM))

cfaj 11-11-2007 09:31 AM

Quote:

Originally Posted by SiegeX (Post 2955052)
Minor tip, you can actually remove the '$' in front of the variables when its inside the $(( )) construct and just do:

Code:

FINAL=$((NUM * NUM))


You can, and it does conform to the POSIX specification, but I
recommend against it.

Until recently, the wording in the spec was not clear, and the
generally POSIX-conforming ash and *BSD shells require the
dollar sign. The extra few characters are insignificant, and make
moving to a different shell easier and safer.

Even if now you only use bash or ksh, there may come a time when you
need to use the more generic shells. It makes sense to use code that
is as bulletproof as possible.



All times are GMT -5. The time now is 11:35 AM.