• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1278
  • Last Modified:

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!
0
dcallot
Asked:
dcallot
  • 3
  • 2
  • 2
  • +3
1 Solution
 
ozoCommented:
$!=3, die $usage;
0
 
ozoCommented:
or perhaps
$! = 22
or
$! = 7
would be more appropriate
0
 
TintinCommented:
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



0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
dcallotAuthor Commented:

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
0
 
TintinCommented:
There's something very strange going on here.

What does

type perl

and perl -v

say?
0
 
jmcgOwnerCommented:
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.
0
 
dcallotAuthor Commented:
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.
0
 
jmcgOwnerCommented:
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.



0
 
manav_mathurCommented:
Shouldnt it initially be

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

instead of

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


Manav
0
 
jmcgOwnerCommented:
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).
0
 
manav_mathurCommented:
Im sorry,

dunno what I was thinking!!

Manav

0
 
moduloCommented:
PAQed with no points refunded (of 50)

modulo
Community Support Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now