1820591 Members
1829 Online
109626 Solutions
New Discussion юеВ

global variables in ksh

 
David Selvaraj
Occasional Contributor

global variables in ksh

How do I get a value of a variable of a
child shell in the parent shell. Kind of global variables in all other program lanugages.
Anyhelp would be appreciated.

Suppose if I have script like this

-- Listing of a one.ksh

#!/bin/ksh
status=0
(
echo "Inside the sub shell"
echo "Call a Sample program "
./sample.ksh
status=$?
echo value of the status inside the
sub shell $status
)
echo value of the status outdside
subshell $status


-- Listing of sample.ksh
ls xyz > /tmp/one.txt

$./one.ksh
Inside the sub shell
Call a Sample program
xyz not found
value of the status inside the sub
shell 2
value of the status outdside subshell 0

thanks.
-- David

6 REPLIES 6

Re: global variables in ksh

Remember that a sub-shell is running as a seperate *process* and therefore in a seperate address space - so a global variable isn't global across these sub-shells - your only option is some form of inter-process communication, using files, signals, pipes etc.

You could try something like this...

(
my_script.sh
echo $?
) | read retval
echo "Return code was $retval"


But of course you'd have to make sure that nothing in the sub-shell sent anything to stdout or stderr.


HTH

Duncan

I am an HPE Employee
Accept or Kudo
harry d brown jr
Honored Contributor

Re: global variables in ksh

You can't return a CHILD process variable back to a parent, but you can pass a PARENT process variable to a child process using "export VARNAME".

You can SOURCE the child process in the parent process:

. ./sample.ksh

live free or die
harry
Live Free or Die
Rui Soares
Valued Contributor

Re: global variables in ksh

Hi!
You can try this:

#!/bin/ksh
status=0
(
echo "Inside the sub shell"
echo "Call a Sample program "
./sample.ksh
status=$?
echo value of the status inside the sub shell $status
exit $status
)
status=$?
echo value of the status outdside subshell $status

Above, I just capture the "status" value that I've set inside the sub-shell with the 'exit' command.

Good Luck!

Rumagoso
Be Well
A. Clay Stephenson
Acclaimed Contributor

Re: global variables in ksh

The simple answer is that you can't; by design the data flow is one way. Perhaps the easist method is to use a temp file (or a named pipe). Something like this:

# Create PID dependant temp file names
TDIR=${TMPDIR:-/var/tmp}
PID=${$}
TFILE1=${TDIR}/X${PID}_1.tmp

export TFILE1

child.sh
STAT=$?
# child.sh would then write whatever it wants to the exported pathname ${TFILE1}

# now read the temp file for three variables

if [ ${STAT} -eq 0 -a -f ${TFILE1} ]
then
read A B C < ${TFILE1}
fi

rm -f ${TFILE1}

Multiple child processes could write to separate temp files.

Using the exit status of a child process is very limited; you can only return a limited rangle of NUMERIC values and you can only return at most 1 value. The temp file method is much more flexible.



If it ain't broke, I can fix that.
harry d brown jr
Honored Contributor

Re: global variables in ksh

One way to CHEAT is using the SOURCE option:

parent1.ksh
#!/usr/bin/ksh
./child1.ksh
. ./child1.vars
echo "value of my_gotit is " $MY_GOTIT
echo "value of my_path is " $MY_PATH


child1.ksh
#!/usr/bin/ksh
export MY_GOTIT=99962
export MY_PATH=/path/to/nowhere
env | grep "^MY_" >child1.vars


Note, that in the PARENT1.KSH script, the use of ". ./child1.vars" this SOURCE's in the variables created by the CHILD process. Also note that I used MY_ as a prefix for my variable names so that in the CHILD process I can grep them easily out of the command returned by env.

You might also want to use the CHILD's PID as a suffix for the child.vars filename making it unique for the instance it is running.

live free or die
harry
Live Free or Die
David Selvaraj
Occasional Contributor

Re: global variables in ksh

Thanks for all your reply.
I'll go ahead and use temp file logic.



-- David