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 07-11-2012, 04:35 AM   #1
themanwhowas
Member
 
Registered: Nov 2005
Distribution: CentOS 5, Fedora 23
Posts: 216

Rep: Reputation: 29
bash script unexpected end of file


Hello

I'm trying to run a simple script that I made about 2 years ago. It used to work fine and I don't think anything has changed (same distro and version too) but now I recieve 'unexpected end of file' when I run the script.

Code:
#!/bin/bash

i=1
while [ $1 -le 20 ]
do
sleep 5
xwd -root > ./xwd/`echo $1`.xwd
i=`expr $1 + 1`
done

I can't see the problem. Please help
 
Old 07-11-2012, 05:09 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
That script doesn't make sense. you do a while checking $1 but $1 will *never* change... you only change $i. surely this has never worked?? There's a hell of a lot of redundant code for a simple script. your logic is a little vague, but this seems to be what you want, and you've somehow got your i's and 1's mixed up:

Code:
#!/bin/bash

for i in $(seq 1 20)
do
sleep 5
xwd -root -out ./xwd/$i.xwd
done
 
Old 07-11-2012, 05:17 AM   #3
rosehosting.com
Member
 
Registered: Jun 2012
Posts: 222

Rep: Reputation: 62
Quote:
Originally Posted by acid_kewpie View Post
That script doesn't make sense. you do a while checking $1 but $1 will *never* change... you only change $i. surely this has never worked?? There's a hell of a lot of redundant code for a simple script. your logic is a little vague, but this seems to be what you want, and you've somehow got your i's and 1's mixed up:

Code:
#!/bin/bash

for i in $(seq 1 20)
do
sleep 5
xwd -root -out ./xwd/$i.xwd
done
totally agreed with this.

anyhow, i copied/pasted the script and it worked without issues here. it seems the script contained DOS newlines or characters and that's why it may be failing with 'unexpected end of file'.

I tried to

Code:
perl -pi -e 's#\n#\r\n#g' /tmp/testLQ
and it failed with the same error message.

then again, replaced DOS newlines to *NIX and it worked

Code:
perl -pi -e 's#\r\n#\n#g' /tmp/testLQ

Last edited by rosehosting.com; 07-11-2012 at 05:24 AM.
 
Old 07-11-2012, 06:01 AM   #4
themanwhowas
Member
 
Registered: Nov 2005
Distribution: CentOS 5, Fedora 23
Posts: 216

Original Poster
Rep: Reputation: 29
Yes, perhaps DOS newlines could be the problem. I edited the path of the redirect in windows just prior to using the script.

The I's and 1's were from me typing the code here instead of copy/pasting


Thanks. I'll try it in a bit

Last edited by themanwhowas; 07-11-2012 at 06:04 AM.
 
Old 07-11-2012, 10:41 AM   #5
wpeckham
Senior Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 1,660

Rep: Reputation: 584Reputation: 584Reputation: 584Reputation: 584Reputation: 584Reputation: 584
No arguments here

Just wanted to say that the preferred way to eliminate those DOS line endings would be to run 'dos2unix' against the file.
 
Old 07-11-2012, 12:51 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: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by acid_kewpie View Post
Code:
#!/bin/bash

for i in $(seq 1 20)
You don't even need seq here. bash can generate the numbers internally with brace expansion or a c-style for loop.

Code:
for i in {1..20}; do
	....
done

for (( i=1 ; i<=20 ; i++ )); do
	...
done
Edit:
expr, as used in the OP, is also unneeded. bash has full integer arithmetic ability built-in.

And $(..) is highly recommended over `..`

Last edited by David the H.; 07-11-2012 at 12:58 PM. Reason: addendum
 
Old 07-11-2012, 04:45 PM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
yep, that's 3 characters saved! Some built-ins i just can never remember.
 
Old 07-12-2012, 06:15 AM   #8
wpeckham
Senior Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 1,660

Rep: Reputation: 584Reputation: 584Reputation: 584Reputation: 584Reputation: 584Reputation: 584
Always nice, but about saved characters......

It is rarely a bad thing to save characters, but in this case that is trivial. The more important factor in using the full power of your shell instead of external tools is reduced load on disk I/O, memory, and CPU. Not having to load external programs during a shell script run means not having to load that file from disk, not having it in memory, not having to load off parameters and return results, and not having to take the TIME for those operations.

Before BASH got so good, this was a fair reason for automating using PERL rather than shell scripts: you could do everything within the PERL script because of the engines POWER! BASH has gotten good enough to have many of the same advantages.

For a one-off script running once the difference may not matter. If you may end up running the script in a loop, under schedulers, thousands of times per day, the difference is significant.

Additional advantages of proper scripting are robustness (you cannot break the script by renaming an external tool, if it does not CALL that external tool) and portability (it should run anywhere a near version of that shell is available) without regard to the behavior changes or version differences of the external tools.

Perhaps most important: the habits of mind. When the person making the script finds value in making his (or her, or its) scripts self-sufficient, correct, and efficient they do better work and are likely to find better ways to solve problems.

On longer scripts you will want to spend off some characters to comment your script to make it easily maintainable, and that is a good thing. You will not want to make it less efficient or less correct, even if that were to save characters.
 
  


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
bash script syntax error: unexpected end of file? smecnb Linux - Newbie 11 02-25-2013 01:44 AM
[SOLVED] Bash Script syntax error: unexpected end of file pasd Linux - Software 11 03-26-2012 08:21 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
Bash script giving unexpected end of file ewebza Programming 4 04-24-2010 09:50 PM
Bash script - syntax error: unexpected end of file Mr Pink Programming 7 12-19-2008 07:31 AM


All times are GMT -5. The time now is 07:46 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