LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-06-2012, 02:54 PM   #1
Kubik
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Rep: Reputation: Disabled
Unexpected end of file


Hello guys,

I have seen here this problem many times but I can't find solution for my issue.

I have this simple script:

Code:
#!/bin/bash
counter=5
factorial=1
while [ $counter -gt 0 ]; do
   factorial=$(( $factorial * $counter ))
   counter=$(( $counter - 1 ))
done
echo $factorial
I run it in putty and i got this error:
./fakt.sh: line 9: syntax error: unexpected end of file

This is pretty strange since this script has only 8 lines... I just cant fix it. Does anybody have any idea?

Thank you
 
Old 10-06-2012, 02:59 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
Have you a newline after the echo $factorial line? If not, add one (just press Enter) and try it again.
 
Old 10-06-2012, 03:09 PM   #3
Kubik
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thank you but it didn't help.

While loop looks ok?
 
Old 10-06-2012, 04:39 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,143

Rep: Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350
You've got DOS end-of-line sequences (\r\n) in that file. I just copied that script, deliberately inserting the '\r' characters, and got exactly that error message.

Use dos2unix to convert the file to UNIX format.
 
1 members found this post helpful.
Old 10-06-2012, 05:17 PM   #5
Kubik
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
OK, thank you!

It helped, really thanks!

Last edited by Kubik; 10-06-2012 at 05:43 PM.
 
Old 10-07-2012, 08:06 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
The script can be more cleanly written like this, BTW:

Code:
#!/bin/bash
counter=5
factorial=1

while (( counter )); do
   (( factorial = factorial * counter ))
   (( counter-- ))
done
echo "$factorial"
Or use a c-style for loop instead:

Code:
for (( counter=1; counter<=5; counter++ )); do
	(( factorial = factorial * counter ))
done
arithmetic expressions
c-style for loop
 
  


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
unexpected end of file btap Programming 14 09-30-2012 01:19 PM
Running a Crafted bash script yelds 'Unexpected end of file' at the end of the file MCLover1337 Linux - General 5 10-15-2011 09:29 AM
unexpected end of file valerio orfano Linux - Newbie 4 12-29-2010 07:16 AM
[bash] unexpected end of file waqasdaar Programming 6 04-03-2009 10:31 AM
Unexpected end of file??? raid517 Linux - Software 2 12-01-2004 05:10 AM


All times are GMT -5. The time now is 11:06 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration