/bin/sh: How to retain variables set in a while loop?

Darin McGrew mcgrew at ichthous.Eng.Sun.COM
Thu Mar 7 09:34:53 AEST 1991


In article <9209 at exodus.Eng.Sun.COM> I write:
>I'd try avoiding putting the while loop into a subshell by doing
>the io redirection in the parent shell.  Your example becomes--
>
>    exec < $UIDRANGES
>    num=0
>    while [ $num -le $choice ]
>    do
>         read minuid maxuid title
>         echo "DEBUG: $num      $title  $minuid         $maxuid"
>         num=`expr $num + 1`
>    done < $UIDRANGES
> 
>    echo "After while: num=$num title=$title minuid=$minuid maxuid=$maxuid"

This should really be--

    exec < $UIDRANGES
    num=0
    while [ $num -le $choice ]
    do
         read minuid maxuid title
         echo "DEBUG: $num      $title  $minuid         $maxuid"
         num=`expr $num + 1`
    done
 
    echo "After while: num=$num title=$title minuid=$minuid maxuid=$maxuid"

since the point was to eliminate the io redirection of the while
loop.

                 Darin McGrew
           mcgrew at Eng.Sun.COM   "Christianity isn't a crutch; it's a stretcher.
       Affiliation stated for    You can't even hobble into heaven."
identification purposes only.



More information about the Comp.unix.shell mailing list