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

I want to do this in a shell script:

#!/usr/bin/ksh

PERL_PATH=/usr/local/bin/perl

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

echo $RET1

How should I do it?

calling the perl script as above gives me an error:

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

EDIT: inside the perl script there are the following statements:

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;
}
+3
source share
3 answers

The assignment RET1in your shell script runs the Perl command and captures its standard output. To keep your Perl program moving forward, change the conditional expression at the end to

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

Startup is done using

1

Another way is to use the exit status of the Perl program. C shell.shcontaining

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

and changing the last condition in missing_months_wrap.plto

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

you get the same result:

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

Perl script, ( stdout), script.

+2

script Perl script $? Perl script, .

perl test.pl
VAR=$?

$? Perl script, .

VAR=`perl test.pl`

  VAR = $(perl test.pl)

, , 0 255.

+2

Source: https://habr.com/ru/post/1744775/


All Articles