LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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-07-2007, 07:15 PM   #1
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282

Rep: Reputation: 30
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.
 
Old 11-07-2007, 07:32 PM   #2
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by snowman81 View Post
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
 
Old 11-07-2007, 07:38 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Also how about

$NUM * $NUM = $FINAL ???

did you mean

$FINAL = $NUM * $NUM
 
Old 11-07-2007, 07:52 PM   #4
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282

Original Poster
Rep: Reputation: 30
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"
 
Old 11-07-2007, 07:56 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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
 
Old 11-07-2007, 08:07 PM   #6
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282

Original Poster
Rep: Reputation: 30
Excellent, it works. Thank you. Could you explain why it works though? I mean, what exactly the parentheses did and everything?
 
Old 11-07-2007, 08:47 PM   #7
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by snowman81 View Post
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 ))
 
Old 11-08-2007, 06:50 AM   #8
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
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
 
Old 11-08-2007, 07:48 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
Old 11-08-2007, 09:09 AM   #10
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282

Original Poster
Rep: Reputation: 30
Ahhh, ok, got it. Thanks guys.
 
Old 11-11-2007, 05:42 AM   #11
SiegeX
Member
 
Registered: Jul 2004
Location: Silicon Valley, CA
Distribution: Slackware
Posts: 171

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

Code:
FINAL=$((NUM * NUM))
 
Old 11-11-2007, 09:31 AM   #12
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by SiegeX View Post
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.

 
  


Reply



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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
bash "unexpected end of file" script error Runge_Kutta Linux - General 6 05-23-2007 03:36 PM
got a syntax error which shows unexpected end of line when tried to run a shell scrip racer_mec Linux - Newbie 1 01-10-2005 01:43 AM
Can I redirect script output to a file without ">> $LOGFILE" at the end of each line davee Linux - General 1 12-19-2003 05:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:03 PM.

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
Open Source Consulting | Domain Registration