Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Capturing Perl exit codes in UNIX ksh so ksh can abend

Posted on 2004-10-09
13
Medium Priority
?
1,263 Views
Last Modified: 2008-02-01

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
Comment
Question by:dcallot
  • 3
  • 2
  • 2
  • +3
13 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 12268786
$!=3, die $usage;
0
 
LVL 85

Expert Comment

by:ozo
ID: 12268828
or perhaps
$! = 22
or
$! = 7
would be more appropriate
0
 
LVL 48

Expert Comment

by:Tintin
ID: 12269533
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:dcallot
ID: 12276821

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
 
LVL 48

Expert Comment

by:Tintin
ID: 12282952
There's something very strange going on here.

What does

type perl

and perl -v

say?
0
 
LVL 20

Expert Comment

by:jmcg
ID: 12284617
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
 

Author Comment

by:dcallot
ID: 12287298
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
 
LVL 20

Expert Comment

by:jmcg
ID: 12365693
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
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12503498
Shouldnt it initially be

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

instead of

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


Manav
0
 
LVL 20

Expert Comment

by:jmcg
ID: 12503971
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
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12504307
Im sorry,

dunno what I was thinking!!

Manav

0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12911682
PAQed with no points refunded (of 50)

modulo
Community Support Moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question