开发者

How can I return a value from a Perl program to a Korn-shell script?

I want to do this i开发者_如何学Pythonn a shell script:

#!/usr/bin/ksh

PERL_PATH=/usr/local/bin/perl

RET1=$(${PERL_PATH} missing_months_wrap.pl)

echo $RET1

How do i do it?

calling the perl script as above is giving me an error:

> shell.sh
Can't return outside a subroutine at missing_months_wrap.pl line 269.

EDIT: inside the perl script the statements are :

unless (@pm1_CS_missing_months)
{
$SETFLAG=1;
}

my @tmp_field_validation = `sqlplus -s $connstr \@DLfields_validation.sql`;

unless (@tmp_field_validation)
{
$SETFLAG=1;
}

if ($SETFLAG==1)
{
return $SETFLAG;
}


You would need to modify your Perl script so that it outputs the value that you need (to stdout) and you can then use that value in your shell script.


The shell script can retrieve the exit status from the Perl script in the $? variable, or the output of the Perl script if it's invoked with backticks or subshell.

perl test.pl
VAR=$?

Be sure to get the $? value right after the Perl script invokation as it may change.

VAR=`perl test.pl`

or VAR=$(perl test.pl)

With the second method, the variable can be a string, with the first one, it has to be an integer value between 0 and 255.


The assignment to RET1 in your shell script runs the Perl command and captures its standard output. To make your Perl program go along, change the conditional at the end to

if ($SETFLAG==1)
{
  print $SETFLAG;
}

Running it produces

1

Another way is to use the exit status of the Perl program. With shell.sh containing

#! /usr/bin/ksh
RET1=$(${PERL_PATH} missing_months_wrap.pl)
echo $?

and changing the last conditional in missing_months_wrap.pl to

if ($SETFLAG==1)
{
  exit $SETFLAG;
}

you get the same output:

$ PERL_PATH=/usr/bin/perl ./shell.sh 
1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜