What's wrong with this Bourne shell script?

Peter da Silva peter at ficc.ferranti.com
Wed Aug 8 05:01:00 AEST 1990


In article <1990Aug3.193231.3166 at silma.com>, aab at silma.com (Andy Burgess) writes:
> total=0
> ls -ld * | while read line
	...
> echo "Total $total"

Unfortunately, that while loop is run by a subshell. Try this:

total=0
ls -ld * | {
	while read line
	...
	echo "Total $total"
}

Or (better) use AWK:

ls -ld * | awk '{
		total=total+$4;
		print "Subtotal " total
	}
	END { print "Total " total }'

(Someone will come up with a PERL version, 'tis certain)
-- 
Peter da Silva.   `-_-'
+1 713 274 5180.   'U`
<peter at ficc.ferranti.com>



More information about the Comp.unix.questions mailing list