Solved

Capturing Perl exit codes in UNIX ksh so ksh can abend

Posted on 2004-10-09
13
1,220 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 84

Expert Comment

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

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl match and sort unique result 2 136
Any syntax error for this clone.plscript 6 139
combine multiple lines 2 60
Matching a random pattern with one common character 2 61
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

773 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