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

I am using system function of perl to call some system function of unix like grep or find. I am redirecting output of unix command in one text file. Out put is generated correctly but status return by perls system function is 256. I have read that system function did not return always '0' ZERO for success and non zero values for error. so how can I check whether my command is successful or not

Replies are listed 'Best First'.
Re: Problem in System function
by exussum0 (Vicar) on May 25, 2004 at 13:21 UTC
Re: Problem in System function
by jeffa (Bishop) on May 25, 2004 at 13:23 UTC

    It has been over a year since i last delt with system call return values, but if my memory server me correct, you need to right shift the status return. When i last coded this, i wrote a forking server that retrieved exit codes via the $? variable:

    my $status = $? >> 8;
    Using a little one liner, we can quickly see what 256 >> 8 is:
    perl -le'print 256 >> 8'
    Which yields 1 -- looks to me as if your invoked program is indeed returning a non zero return code. Are you also capturing STDERR?

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      perl -le'print 256 >> 8' returns 1 so 1 is failure or success?

        You can only say one thing about a return code of 1 ... it is not 0.

        Zero is conventionally "success" ... it means "there was no error to be reported" ... hence, zero.

        Everything else is treated as Not 0 ... it could mean failure, it could mean a warning, it could mean success. It is up to the person(s) who wrote the code you are running. This is why i think it is vital to capture STDERR as well.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: Problem in System function
by calin (Deacon) on May 25, 2004 at 13:24 UTC

    See perldoc perlvar for $?

    Basically, $exitval = $? >> 8.