Can a variable assigned in a shell function be accessed outside

Hi

I have the following script :

#!/bin/ksh
compare()
{
cat $1>t1
cat $2>t2
cy1=`cut -f13 -d'�' t1`
cy2=`cut -f13 -d'�' t2`
print "cy1 = $cy1"
print "cy2 = $cy2"
if [ $cy1 = $cy2 ]
then
  echo "yes"
else
  echo "no"
fi
}
 
d3=$(compare temp1.rm temp2.rm)
echo $cy1 <== Here the value assigned inside compare() should be displayed.

But I get blank value.
Any thoughts....Thanx in advance.. Am using SunSolaris, 5.8 and executing this in k-shell

Please show us your input files and display the value of the d variable.

Jean-Pierre.

OK..Lemme be clear,
My script:

#!/bin/ksh
compare()
{
cat $1>t1
cat $2>t2
cy1=`cut -f13 -d'�' t1`
cy2=`cut -f13 -d'�' t2`
if [ $cy1 = $cy2 ]
then
  echo "yes"
else
  echo "no"
fi
}
d3=$(compare temp1.rm temp2.rm)
print $d3 <== Prints "yes" as cy1 and cy2 are equal.
echo $cy1 <== Is null.

I need to manipulate some part of the script using value of d3 and use value of cy1 down the script. How can I use the value for cy1 later??

Thx for the help !!!!

Are you sure that cy1 and cy2 are not both null ?
In that case d3 will contains yes.

For debug purpose, modify your script :

compare()
{
   cy1=`cut -f13 -d'�' $t1`
   cy2=`cut -f13 -d'�' $t2`
   if [ $cy1 = $cy2 ]
   then
      echo "yes, values='$cy1/cy2'"
   else
      echo "no, values='$cy1/$cy2'"
   fi
}
d3=$(compare temp1.rm temp2.rm)
print "d3='$d3'"
print "values='$cy1/$cy2'"

You can use cy1, cy2 and d3 later in your script.

Jean-Pierre.

Your problem is due to the fact that the function is executing in a subshell due to the use of $(...) syntax - not in the current process.

Here is a simplified example

#!/bin/ksh93

compare()
{
   var=3
}

d=$(compare)
echo "Var=${var}"

compare
echo "Var=${var}"

:b: Sometimes I must be blind.

Jean-Pierre/

Thanx fpmurphy and aigles.. Got it worked..