Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!aero-c!usc!samsung!munnari.oz.au!brolga!lingua.cltr.uq.OZ.AU!root From: root@lingua.cltr.uq.OZ.AU (Hulk Hogan) Newsgroups: comp.unix.shell Subject: /bin/sh: How to retain variables set in a while loop? Summary: how do I keep the values of variables? Keywords: sh, while loop, variables Message-ID: <1991Mar5.025314.2317@lingua.cltr.uq.OZ.AU> Date: 5 Mar 91 02:53:14 GMT Distribution: comp Organization: Centre for Language Teaching and Research, Uni of Queensland, AUSTRALIA. Lines: 34 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@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....."