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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-07-2007, 07:15 PM
|
#1
|
Member
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282
Rep:
|
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.
|
|
|
11-07-2007, 07:32 PM
|
#2
|
Member
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221
Rep:
|
Quote:
Originally Posted by snowman81
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:
|
|
|
11-07-2007, 07:38 PM
|
#3
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
Also how about
$NUM * $NUM = $FINAL ???
did you mean
$FINAL = $NUM * $NUM
|
|
|
11-07-2007, 07:52 PM
|
#4
|
Member
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282
Original Poster
Rep:
|
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"
|
|
|
11-07-2007, 07:56 PM
|
#5
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
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
|
|
|
11-07-2007, 08:07 PM
|
#6
|
Member
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282
Original Poster
Rep:
|
Excellent, it works. Thank you. Could you explain why it works though? I mean, what exactly the parentheses did and everything?
|
|
|
11-07-2007, 08:47 PM
|
#7
|
Member
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221
Rep:
|
Quote:
Originally Posted by snowman81
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 ))
|
|
|
11-08-2007, 06:50 AM
|
#8
|
Member
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150
Rep:
|
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.
Last edited by Hobbletoe; 11-08-2007 at 08:15 AM.
Reason: D'oh! Thanks, Colucix
|
|
|
11-08-2007, 07:48 AM
|
#9
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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
|
|
|
11-08-2007, 09:09 AM
|
#10
|
Member
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282
Original Poster
Rep:
|
Ahhh, ok, got it. Thanks guys.
|
|
|
11-11-2007, 05:42 AM
|
#11
|
Member
Registered: Jul 2004
Location: Silicon Valley, CA
Distribution: Slackware
Posts: 171
Rep:
|
Minor tip, you can actually remove the '$' in front of the variables when its inside the $(( )) construct and just do:
Code:
FINAL=$((NUM * NUM))
|
|
|
11-11-2007, 09:31 AM
|
#12
|
Member
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221
Rep:
|
Quote:
Originally Posted by SiegeX
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:25 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|