http://qs321.pair.com?node_id=1091036

gouldopfl has asked for the wisdom of the Perl Monks concerning the following question:

I have a ksh script on Sun Solaris. We are running 5.10.0 of Perl. I have no control over this. The ksh script calls a perl program which recursively goes through a series of directories to get all of the data that has not been posted. I did not write this nor have I had a lot of experience with perl as that is not our main programming language here. The way the program is written is if the login fails it still returns a 0 error code even with a die statement, or at least the ksh script thinks it does and continues. I need to be able to have the perl program send a 0 or 1 to the ksh script to know whether to continue or not. Any idea's or examples would be greatly appreciated. Best Regards Michael Gould

Replies are listed 'Best First'.
Re: perl program return error code to ksh
by Corion (Patriarch) on Jun 24, 2014 at 11:20 UTC

    die sets the exit code to a nonzero value, which you should find in $? in Perl and in ksh. Most likely you are calling perl with an intermediate shell, or maybe within a pipe construct:

    myscript | grep errors

    Pipe constructs tend to lose the intermediate exit codes.

    If you can show the relevant part of the shell script where you call Perl and where you check for the error, maybe a solution will become apparent.

Re: perl program return error code to ksh
by salva (Canon) on Jun 24, 2014 at 11:20 UTC
Re: perl program return error code to ksh
by blue_cowdawg (Monsignor) on Jun 24, 2014 at 14:20 UTC
        returns a 0 error code even with a die statement

    I'm pretty sure die is returning a nonzero value. That's how it is designed. Here is what I mean:

    ~# perl -e 'die "trying"' trying at -e line 1 ~# echo $? 255
    As mentioned elsewhere the Perl script may be getting executed in such a way that the return code is obfuscated. Something like:
    myscript.pl | do_something
    What does the sniglet of shell code look like that is invoking the Perl script?


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      ${BIN_DIR}/tapeloadListing.pl >> ${LOGS_DIR}/corps.${VERSION_DATE}.l +og #2>&1 if (( $? == 0 )); then echo "PERL script used to get the tapeload snapshot ra +n successfully" else echo "PERL script used to get the tapeload snapshot fa +iled, hence aborting the script as the files can't be downloaded" | m +ailx -s "CORP V2 BUILD ERROR - ${VERSION_DATE}" ${MAIL_ADMIN} exit 1; fi
      The perl code
      $fh = new FileHandle; $fh -> open("< /export/home/$ADMIN/pwds/ftp_pass.txt") or die "Error o +pening FTP password file, $!"; $TAPELOAD_PWD = sprintf <$fh>; $fh -> close; print $TAPELOAD_PWD chomp($TAPELOAD_PWD);
      If I put in a totally incorrect user in the perl code, the KSH script still continues to run which I want to exit immediately.

        add some code tags pretty please?


        Peter L. Berghold -- Unix Professional
        Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

        Are you seeing the message:

        Error opening FTP password file, ....

        ?

        Where you say:

        If I put in a totally incorrect user in the perl code, the KSH script still continues to run which I want to exit immediately.

        implies the Perl script error is somewhere else.