passing a variables value from the called script to calling script using ksh

How do i get the value of the variable from the called script(script2) to the calling script(script1) in ksh ?

I've given portion of the script here to explain the problem.

Portion of Script 1

-----
-----
tmp=`a.ksh p1 p2 p3`

if [ $? != 0] then
# error processing
fi
-----
-----
# I want to use the $PID(from the script2 here)

Portion of Script 2

-----
-----
# few processing
PID=SOMEVALUE ASSIGNED

If I use return (exit PID), the problem is, I can not check the script failure or success.
I want to use only the variable PID in script1 not the other variables.

Thanks for your help.

You have this in your script:

PID=SOMEVALUE ASSIGNED

Just add the following:

Pid_script2=$PID
export Pid_script2

Pid_script2 ia a global variable now.

PS: I am assuming script2 runs first.Also you will have to source script1 and script2 so that it does not run in another shell.

Thanks for the reply.

Actually script1 runs first and calls script2. If you look at the code sample above.

Problem is returning only one variable from script2 at the same I should be able to check the script2 run status using $?.

You can have all the variable by executing the script2 inside script1 using . scriptname (inline). The problem is I've same set of variables in script1 and script2, both uses another file for sourcing the common variables.

Have the child script open a file, write whatver you want defined., like this:

#inside child script
mypid=$$
echo "PID=$mypid" > /tmp/pidfile
# the pid of the child is now recorded in /tmp/pidfile
#parent process
tmp=$(child.ksh 1 2 3 )
# now read the file child.ksh wrote
chmod +x /tmp/pidfile 
. /tmp/pidfile
# The variable PID is now defined in the parent

The only other way to do this is using co-processes, which do not seem to match your model.

You could "print" the value to the standard output in script 2:
print $PID or echo $PID
In script1 get it to a variable:
tmp=`script2 p1 ...`
The tmp variable will have your value.
You can do the same if you have to return more than 1 value.
They will be all in the tmp variable separated by space. To use them put them into "parameters" (don't forget to save true parameters of script 1):
set $tmp
Now $1 will have the return value 1, $2 - the return value 2, etc.

I understand the concept of writing it to standard output and assign the value to a variable. Can you show me some code sample to display $1 $2 of the variable ?

Jim, Your suggestion is a good workaround.

# script1
tmp=`script2 $1 $2`
RESULT=$?
if (($RESULT != 0 )) then
exit $RESULT
fi
set $tmp
print $1 $2 $3

#script2
if [[ $TEST = "" ]] then
print myparms $1 $2
else
exit 1
fi

# After the following step we expect to see "myparms par1 par2"
script1 par1 par2
#set TEST to fail script2
export TEST=YES
script1 par1 par2
print script1 exit code is $?
unset TEST

Sorry, I don't understand your example or it does not meet my requirement.

  1. Second script does not have any variable set and that variable is not displayed in the first script.
  2. We also need to check whether the 2nd script run fine or not.

No. Script2 runs first.

Hi,

In this thread a code was suggested by Jim in this manner

#inside child script
mypid=$$
echo "PID=$mypid" > /tmp/pidfile
# the pid of the child is now recorded in /tmp/pidfile
#parent process
tmp=$(child.ksh 1 2 3 )
# now read the file child.ksh wrote
chmod +x /tmp/pidfile 
. /tmp/pidfile
# The variable PID is now defined in the parent

I have a couple of questions:

  1. Is the /tmp/ directory automatically created?
  2. from the parent script, what does this line do:
    tmp=$(child.ksh 1 2 3 )
    does it re-execute the child.ksh script?
  3. is it ./tmp/pidfile or is there really a space there?