tags:

views:

203

answers:

5

I have a ksh script that calls a Perl program. The Perl program creates some important data that the ksh script needs to act on. Example:

My ksh program:

#!/usr/bin/ksh
abc.pl > $logFile
# perl pgm creates variable $importantData   See below.
#  HOW DO I GET THE .KSH SCRIPT TO SEE $importantData  ???
def.ksh $importantData  # send important data to another .ksh script
exit

My Perl program:

$importantData = somefunction();
exit;
A: 

Couldn't you just have the perl script print out the data and then pipe the data to the new ksh script

#perl script
print someFunction();

#ksh script
perl abc.pl | ksh scrip
Mimisbrunnr
The .ksh does stuff before, and after, the perl call, and, also, it does logging (see next answer), so this won't work. But a good thought.
Dave
A: 

The Perl program should print the variable. The KSH script captures the output.

Perl...

$importantData = somefunction();
print $importantData;
exit

KSH...

#!/usr/bin/ksh
abc.pl > $logFile
importantData=$(perlprogram)
def.ksh $importantData  # send important data to another .ksh script
exit
Robert Wohlfarth
Looks like you have TWO perl pgms in your solution? abc.pl in the second line, and 'perlprogram' in the third line.
Dave
A: 

If you cannot use standard output or standard error for providing $importantData, you can use a temporary file.

mouviciel
Yes, this looks like the way to go, for what I'm doing, since there are more disadvantages to the other four suggestions I see to this point.
Dave
A: 

If the value is numeric, you can return it using Perl's exit() function, which would appear as Perl's status code.

for instance, exit(3) will cause Perl to return the value 3 to ksh.

As far as I understand, you can use ksh's special $? variable to read the exit status of the last command. See here for more details.

Nathan Fellman
Nope, not numeric, plus the perl pgm can return a true error code.
Dave
+1  A: 

On *nix systems a child process can not modify it's parent's environment. If you design your perl script to print the important data to standard out you'll have several ways to capture it for later processing.

The simplest is to redirect the output to a file for later processing:

perlprogram > saved.output

If you only need the data for one process, and that process is the next thing to do, you can pipe the perl output to the other process' input:

perlprogram | otherprogram

You can also save the output into an environment variable (note the parent script is doing the saving not the child):

envvariable=$(perlprogram)

Or old school (which has quoting problems with complex commands):

envvariable=`perlprogram`
Robert Menteer