Re: recursive grep problem [zip logs]



On 1 Dec 2007, Tony van der Hoff outgrape:

On 29 Nov at 12:38 Chris Davies <chris-usenet@xxxxxxxxxxxx> wrote in message
<tfo225x38f.ln2@xxxxxxxxxxxxxxxxx>
DATE=`date +'%Y%m%d'`; find t -name '*log' | while IFS= read FILE; do
mv "$LOG" -f "${LOG}_$DATE" && bzip2 "${LOG}_${DATE}"; done
[...]
Don't quite see how the last one works, though...

It's a useful trick. The find spits out a stream of filenames separated
by linefeeds: each line is then consumed by a `read' (with IFS reset to stop
field splitting; you probably want -r to stop backslashes being interpreted
as well); the read will return success until there's nothing left to read, so
the effect is to execute the interior of the loop with each thing returned from
the find in turn.

If you're careful you can use this with bidirectional pipes and
coprocesses to implement full-blown parsers in the shell (but look out:
it's hard to change variable values in such loops because they're
executed in a subshell: you often have to have them echo variable
assignments on stdout and run the whole bloody thing in an eval $(...)).

--
`Some people don't think performance issues are "real bugs", and I think
such people shouldn't be allowed to program.' --- Linus Torvalds
.



Relevant Pages