On Mar 12, 2012, at 1:20 PM, David Korn <[email protected]> wrote:

> 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 
>>> (also 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 
>>> "find ..|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 the 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 xargs)
>>> 
>>> i don't have -e or -o pipefail turned on, and in any case, the script is 
>>> verified 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.

very interesting, that solves the problem as described

(any particular reason for 5?)

one additional question tho--is it possible to invoke something inside the 
while loop with a stdin of its own, separate from the one used by while and 
read? at the moment, commands inside the while loop seem to have no 0 fd at all

(if you're curious, what I'm invoking inside the loop is an interpreter script 
which will drop me into a REPL if an error occurs, but only if stdin is 
available)

Sent from my iPhone
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users

Reply via email to