Phil M Perry wrote:
> What is the echo "$CONTENTS" | while-command supposed to do? I don't
> think I've seen that idiom before. I suspect that it's creating some sort of

I've used this idiom quite a bit - although not with "echo".  It is useful for
looping over very large output sets, similar to the "find | xargs" trick.  And
I agree with you and Jack - it is almost certainly creating a subshell, at
least in bash.  For the record, it does not seem to create a subshell in zsh.

The option of "for LINE in $CONTENTS" works very well in most cases, but it
has its own problems.  For one thing, it's splitting contents by whitespace,
not newlines.  For another, it has to expand $CONTENTS inline - so in the case
of interpolation of a command with very large output, the shell has to slurp
all of that output into memory before it can start iterating over it.  I would
also expect that it would exceed the maximum command line arguments, but that
doesn't seem to be a problem for bash built-ins.

There may be ways to get around the first problem (whitespace vs newlines)
using judicious use of the IFS variable (which I've never really grokked).  
I've been lead to use the pipe-while-read approach because of the second
problem.  I would be interested to see alternate solutions.

> local environment within it (piping to a new process?) so that a new "N" is
> created just for that little bit. Does $LINE exist after the echo/loop?
> 
> Adam wrote:
> > #!/bin/bash
> > CONTENTS="xyz"
> > N=1
> > echo "$CONTENTS" | while read LINE ; do
> >         N=4
> >         echo N = $N
> > done
> > echo N is now $N
> >
> > [EMAIL PROTECTED] ~]$ ./d
> > N = 4
> > N is now 1
> >   
> 
> _______________________________________________
> Mid-Hudson Valley Linux Users Group                  http://mhvlug.org        
>      
> http://mhvlug.org/cgi-bin/mailman/listinfo/mhvlug                           
> Upcoming Meetings (6pm - 8pm)                         MHVLS Auditorium        
>           
>   Sep 3 - Porkchop - The Areas of My Expertise
>   Oct 1 - Ubikeys
>   Oct 4 - Linux Fest
>   Nov 5 - Releasing Open Source Software
>   Dec 3 - TBD
>   
> 


=============================================================================
michaelMuller = [EMAIL PROTECTED] | http://www.mindhog.net/~mmuller
-----------------------------------------------------------------------------
Society in every state is a blessing, but government even in its best state
is but a necessary evil; in its worst state an intolerable one... 
 - Thomas Paine 
=============================================================================
_______________________________________________
Mid-Hudson Valley Linux Users Group                  http://mhvlug.org          
   
http://mhvlug.org/cgi-bin/mailman/listinfo/mhvlug                           
Upcoming Meetings (6pm - 8pm)                         MHVLS Auditorium          
        
  Sep 3 - Porkchop - The Areas of My Expertise
  Oct 1 - Ubikeys
  Oct 4 - Linux Fest
  Nov 5 - Releasing Open Source Software
  Dec 3 - TBD
  

Reply via email to