Operating System - Linux
1753290 Members
5278 Online
108792 Solutions
New Discussion юеВ

Re: posix shell read loop premature EOF condition

 
Scott Williams_5
Frequent Advisor

posix shell read loop premature EOF condition

Has anyone encountered a situation where a read loop terminated prematurely, as if it hit an EOF in the middle of the file? No special characters were found in the file.

Say, for example, in the loop below, it terminated after line # 18, instead of the last line # 62.
>wc -l file1
62 file1

while read -r LINE
do
#does some work here
done ----------------------------------------------
I know this is very general, but this week-end I ran a script that does this sort of thing, and it looked like it completed successfully, but only 18 out of 62 records were processed in the file.

Grasping for straws :-)

scott

6 REPLIES 6
Steven E. Protter
Exalted Contributor

Re: posix shell read loop premature EOF condition

Shalom Scott,

When a tried and true script that used to work stops, the first thing you want to check is the data.

Maybe do a quick hex dump and see if an unfortunate i/o problem put an EOF a little early.

The while code I'm familiar with and its always worked. Only and EOF character can bust that code according to my experience.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
A. Clay Stephenson
Acclaimed Contributor

Re: posix shell read loop premature EOF condition

I suspect one of three things happened:

1) Your script and the wc commands were run at different times so that the file was only 18 lines long when read by your script. A short file was probably entirely loaded into buffer when the file was opened. This also assumes that both your script and the wc command actually read the same file -- not just files that share a common name.

2) There are non-ASCII characters embedded in your file. Use od or hd -v to make sure.

3) Your script is receiving a signal.
If it ain't broke, I can fix that.
Oviwan
Honored Contributor

Re: posix shell read loop premature EOF condition

Hi

do a set -x at the top of your script, perhaps you can discover the mistake.

Regards
Scott Williams_5
Frequent Advisor

Re: posix shell read loop premature EOF condition

Thanks for all of your responses.

Steven,
Checking the file for binary data was the 1st thing I looked at. Nothing untoward there.

A.Clay,
I think you've got it. The process that creates the file must have still had the file open for output, and when the script ran, there was only 18 records in the buffer. I'll need to beef up the coordination between the two separate processes.

Thanks again to all!

Scott Williams_5
Frequent Advisor

Re: posix shell read loop premature EOF condition

A.Clay,

Appologies -- I meant to give you 9 points, not 4. I can't seem to modify the assigned points now. Is there a way?
Scott Williams_5
Frequent Advisor

Re: posix shell read loop premature EOF condition

It is likely that a process had the file opened for output while the script was reading it.