Link to home
Start Free TrialLog in
Avatar of dcallot
dcallotFlag for United States of America

asked on

Capturing Perl exit codes in UNIX ksh so ksh can abend


I have a UNIX ksh script with multiple steps that calls a Perl script in one of the middle steps.

When the Perl script executes and dies because of some error condition in the Perl script how do I pass a value back to my UNIX ksh script so my ksh script knows to abend?

I guess I'm not clear on what return code die uses when it executes.  My perl code is:

       my $usage="Usage: $0 <DATECARD> <FILE1IN> <FILE1OUT> $!\n";

        @ARGV == 3 or die $usage;

Unix ksh code

      perlscript.pl $file1 $file2 $file3
      echo $?

This echos "0"


Thanks!
Avatar of ozo
ozo
Flag of United States of America image

$!=3, die $usage;
or perhaps
$! = 22
or
$! = 7
would be more appropriate
Avatar of Tintin
Tintin

There is no way possible that using the example you supplied that you would get an exit status of 0.

As the docs for die say:

If $! is 0, exits with the value of "($? >> 8)" (backtick `command` status).  If "($? >> 8)" is 0, exits with 255.

I think there is something you aren't telling us or that you've misinterpreted.

In your ksh script, you can either do:

perlscript.pl $file1 $file2 $file3 || exit 1

or

perlscript.pl $file1 $file2 $file3

if [ $? -ne 0 ]
then
    echo "Perl script failed"
    exit 1
fi



Avatar of dcallot

ASKER


Well, here's the actual code and the output from running the ksh script and calling the perl script.  While the perl script ($PMEXTRACT) is passed 3 arguments the first argument $matdatefile is not defined in the ksh script.  I would expect ksh to abend because the value passed back from die usage in the perl script.  You see its passing back 0 though.

Thanks


  if [ $run = "BATCH" ]
  then
    if [ -f $DATA/ntpa4esi.dat ]
     then
      echo "PM FILE EXISTS"
      $PMEXTRACT $matdatefile $DATA/ntpa4esi.dat $DATA/xsp/pm01_mat.dat
      if [ $? -ne 0 ]
      then
        echo "Perl script failed"
        exit 1
      fi
      cat $DATA/xsp/pm01_mat.dat >> $XSPDATA/xspanncmts.out
    fi
  else
    echo "PM FILE DOES NOT EXIST: $DATA/ntpa4esi.dat"
    exit 1
  fi



[293] + [ BATCH = BATCH ]
[295] + [ -f /app/service/esi/data/ntpa4esi.dat ]
[297] + echo PM FILE EXISTS
[298] + /app/service/esi/scripts/xsp/dual/pm01_fmt.pl /app/service/esi/data/ntpa4esi.dat /app/service/esi/data/xsp/pm01_mat.dat
Usage: /app/service/esi/scripts/xsp/dual/pm01_fmt.pl <DATECARD> <FILE1IN> <FILE1OUT>
[299] + [ 0 -ne 0 ]
[305] + cat /app/service/esi/data/xsp/pm01_mat.dat
[305] + 1>> /app/service/esi/data/xsp/xspanncmts.out
There's something very strange going on here.

What does

type perl

and perl -v

say?
Does your script declare its own die handler (or use a module that does)?

You can certainly execute a 'die' inside an 'eval' and still come out with an exit status of zero, but in that case the text given to 'die' is assigned to the $@ variable inside Perl and not printed out, as shown in your transcript.
Avatar of dcallot

ASKER

perl -v

This is perl, version 5.003 with EMBED
        built under solaris at Nov 14 1996 13:28:28
        + suidperl security patch

Copyright 1987-1996, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5.0 source kit.
While 5.3 is a little old, I know of no reason why the die behavior would be different. You didn't respond to my question about die handlers.

One other thing to try: change your script to read

     then
      echo "PM FILE EXISTS"
      perl $PMEXTRACT $matdatefile $DATA/ntpa4esi.dat $DATA/xsp/pm01_mat.dat
      if [ $? -ne 0 ]
      .
      .
      .

that is, invoke perl to interpret your script rather than letting the shebang line do it. I doubt this will make a difference, but it will get us ready to try things like replacing your invocation with a line like:

   perl -e 'die "Checking for exit code" ; '

to try to track down which part of the exit code reporting apparatus is broken.



Shouldnt it initially be

($#ARGV == 3) or die(".....

instead of

(@ARGV == 3) or die(".....


Manav
Manav -

I think the original code is correct.

@ARGV is being evaluated in a scalar context, so it gives the count of elements in the array. Your alternative, $#ARGV would give the subscript of the last element in the array (which would normally be "2" when there are 3 elements).
Im sorry,

dunno what I was thinking!!

Manav

ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial