variable not retaining value

Bourne shell
Solaris

I'm trying to set a flag to perform an action only when data is found. So I initialize the flag with:

X=0

Then I read the data:

if [ ${exitCd} -eq 0 ]; then
while read a b c
do
X=1
done < ${inputFile}
fi

The problem is that X will be set to 1 inside the while loop but when I exit it goes back to 0. I figured this was some sort of local global thing (even though they were in the same shell) so I tried export X but that didn't help.

Can someone splain this to me? Thanks.

If I change to a korn shell this problem seems to go away. Can anyone edify me?

Pretty cool, huh? The bourne shell uses a subshells every now and then, like for your loop. When the loop is finished, the subshell exits, and the variables disappear like magic. Not only does the superfluous subshell process consume extra resources, it helps make the language nice and unpredictable. The bourne shell has lots more cool surprises like this, so expect a wild ride. :wink:

So even if you export the variable before calling the while loop it goes ahead and creates a local copy of the variable? Well isn't that special. I don't suppose you know a way of circumventing that behavior do you?

Sure do! Switch to ksh.

HAHA, but then I have to retest the rest of the script. Jeez, this job would be a lot easier w/o clients. :wink: