ProgrammingThis 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.
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.
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.
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.
#!/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
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.