What's wrong with this Bourne shell script?

Evelyn C. Leeper ecl at cbnewsj.att.com
Sun Aug 5 22:35:22 AEST 1990


In article <1990Aug3.193231.3166 at silma.com> aab at silma.UUCP (Andy Burgess) writes:
> ----------------------- cut here --------------
> total=0
> ls -ld * | while read line
> do
>   set - `echo $line`
>   total=`expr $total + $4`
>   echo "Subtotal $total"
> done
> echo "Total $total"
> ----------------------- cut here --------------
> At the end total prints as 0!
> What gives?

You cannot export shell variables from a child to a parent.  The while loop
spawns a subshell and "total" is know only to the subshell.  [Note: can
someone arrange to have this put in the "commonly asked questions" posting?]

Evelyn C. Leeper  |  +1 908-957-2070  |  att!mtgzy!ecl or  ecl at mtgzy.att.com
--
The only thing necessary for the triumph of evil is for good men to do nothing. 
                                                            -Edmund Burke



More information about the Comp.unix.questions mailing list