What's wrong with this Bourne shell script?

Scott Yelich ODU scott at tab29.larc.nasa.gov
Tue Aug 7 23:01:49 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?

As it has been said: It should.

However, I fiddle around with SH scripts... and this is how I would hack your script:

1)  I would not do it in perl-- I hate it when people ask about bourne shell and other 
   people reply with perl....  

#!/bin/sh

set - `ls -ld *`
while test "$6"
do
  shift 3
  EXPR="$1 + $EXPR"
  shift 5
done
echo "Total=`expr $EXPR 0 `"

Not that it really matters.... but there isn't a pipe in there nor so many calls to expr...
Of course, I didn't assign the variable total anywhere... unless you are going to use
it somewhere else, it isn't needed and it looked like you were just using it as a counter...

Scott
--
Signature follows. [Skip now]

 -----------------------------------------------------------------------------
 Scott D. Yelich                         scott@[xanth.]cs.odu.edu [128.82.8.1]
 After he pushed me off the cliff, he asked me, as I fell, ``Why'd you jump?''
 Administrator of:    Game-Design requests to <game-design-request at cs.odu.edu>
 ODU/UNIX/BSD/X/C/ROOT/XANTH/CS/VSVN/
 -----------------------------------------------------------------------------



More information about the Comp.unix.questions mailing list