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 |
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.
|
 |
07-11-2012, 04:35 AM
|
#1
|
Member
Registered: Nov 2005
Distribution: CentOS 5, Fedora 23
Posts: 218
Rep:
|
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
|
|
|
07-11-2012, 05:09 AM
|
#2
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
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
|
|
|
07-11-2012, 05:17 AM
|
#3
|
Member
Registered: Jun 2012
Location: Missouri, USA
Posts: 236
Rep:
|
Quote:
Originally Posted by acid_kewpie
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.
|
|
|
07-11-2012, 06:01 AM
|
#4
|
Member
Registered: Nov 2005
Distribution: CentOS 5, Fedora 23
Posts: 218
Original Poster
Rep:
|
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.
|
|
|
07-11-2012, 10:41 AM
|
#5
|
LQ Guru
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 6,086
|
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.
|
|
|
07-11-2012, 12:51 PM
|
#6
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
Quote:
Originally Posted by acid_kewpie
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
|
|
|
07-11-2012, 04:45 PM
|
#7
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
yep, that's 3 characters saved! Some built-ins i just can never remember.
|
|
|
07-12-2012, 06:15 AM
|
#8
|
LQ Guru
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 6,086
|
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.
|
|
|
All times are GMT -5. The time now is 03:14 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
|
|