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

Hulk Hogan root at lingua.cltr.uq.OZ.AU
Tue Mar 5 12:53:14 AEST 1991


Hi. I guess this is probably a FAQ, but here goes anyway...
Platform: Sun4.   OS: SunOS 4.x.
I have written some Bourne shell script which sets some variables within 
the body of a while loop. After the loop finishes, the variables are no
longer set.  How can I retain these values after the while loop?
I've tried an export within the body of the while, but that didn't work.

I remember reading somewhere that the body of a while loop is executed in
sub-shell, and that's why the behavious occurs. I really don't care how
the shell does while loops. I just want my variables!

I can use a combination of head, tail and cut to get the results I
want, but it would be so much easier if I could just use while.

Code fragment to assign field 1 of line number $choice to $minuid and
field 2 of it to $maxuid follows.
| 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 loop: num=$num  title=$title minuid=$minuid maxuid=$maxuid"

/\ndy
-- 
Andrew M. Jones,  Systems Programmer, 	Internet: andy at lingua.cltr.uq.oz.au
Centre for Lang. Teaching & Research, 	Phone (Australia):  (07) 365 6915
University of Queensland,  St. Lucia, 	Phone (World):    +61  7 365 6915
Brisbane,  Qld. AUSTRALIA  4072 	Fax: 		  +61  7 365 7077

"No matter what hits the fan, it's never distributed evenly....."



More information about the Comp.unix.shell mailing list