[lug] In But Not Out Of Loop

S. Luke Jones luke at frii.com
Mon May 28 09:24:16 MDT 2001


Kenneth D. Weinert wrote:
> Do you mean like this?
> 
> ls *.c | while read $file; do
> C = $[$C + $(wc -c $file)]
> done
> 
> echo $C
> 
> 
> if you pipe something through a while loop that happens in a subshell.
> 
> I've been bit by that before.

If you explicitly declare a variable local that will shield it
from the external scope. But IIRC the "local" keyword is valid
only in a function:

	function foo {
		local amount=$1
		local rate=$2
		echo $((amount*rate)) | mutt -s "amount * rate" root
	}

to pick a completely contrived example. The other ones in this
thread aren't especially appealing, IMHO. Maybe there's a scripting
language that's as good at file operations and glueing commands
together as bash and as good at programming stuff (loops, setting
and retrieving variables, etc.) as python or perl, but if so, I
never heard of it.

I saw something by Korn about how ksh93 is really good at that
stuff. (I haven't used ksh in five years or so, and that might have
been pdksh -- i.e. ksh88 -- now that I think about it.) So I don't
know myself, but if ksh93 is the consumate scripting language, then
it needs to hire a publicist.

-- 
Luke Jones = luke/vortex/frii/fullstop/com



More information about the LUG mailing list