Subject: Re: Re: [ast-users] early termination of a "find|while read" loop?
--------
> On 3/11/2012 2:30 PM, Aaron Davies wrote:
> > i'm running into an odd problem at work
> >
> > i have one ksh (93u) script that is effectively a driver for another script
> > (a
> lso ksh 93u)
> >
> > it runs "find" on a directory, and invokes the other script for each file
> >
> > in order to be slightly better at handling odd filenames, i'm trying to use
> > "f
> ind ..|while read" instead of "for f in `find ..`", but the loop is
> terminating
> after only one iteration. if i replace the invocation of the shell script
> with t
> he commands it contains, the loop works as expected. if i use "for", it works
> as
> expected
> >
> > (i need to do a little processing on the names before handing them off to
> > the
> script (which i don't fully control), so it would be much harder to do with
> xarg
> s)
> >
> > i don't have -e or -o pipefail turned on, and in any case, the script is
> > verif
> ied to be exiting successfully
> >
> > any ideas what's going on? i can post an example in a day or two if
> > necessary
>
> This sounds as if the script is reading from stdin. The first line of
> the output of find is consumed by the "read", all the rest of the output
> of find is consumed by the script, and then there is nothing left for
> the next "read". Perhaps replacing "find ... | while read f ; do ...
> done" with "find ... | while read f ; do { ... ; } < /dev/null ; done"
> will solve the issue?
Another alternative is
find ... | while read -u5 f ; do ... done 5<&0-
This will also run faster since the shell is able to do full buffer
reads on file descriptor 5 since it is not shared.
David Korn
[email protected]
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users