Loading ...
Sorry, an error occurred while loading the content.
 

How to get the return code of the unix command that is run in system function

Expand Messages
  • nagaprasadv
    Suppose if i have the command something like this. $rc=system( cat sample;echo $? ) The file sample is not there in the directory which i am executing the
    Message 1 of 2 , Apr 8, 2010
      Suppose if i have the command something like this.

      $rc=system("cat sample;echo $?")

      The file sample is not there in the directory which i am executing the above command so it will through an error and i want to capture that error and display it as echo $? but i am not able to do so.

      when i exeucte the above code echo $? is giving me 0 even the sample file doesn't exist.

      Is there any way i can capture that error code in one system function call.
    • Shlomi Fish
      ... The problem is that echo outputs to the standard output and normally returns a true process exit status code (Unless there s a very unlikely event that it
      Message 2 of 2 , Apr 8, 2010
        On Thursday 08 Apr 2010 13:33:07 nagaprasadv wrote:
        > Suppose if i have the command something like this.
        >
        > $rc=system("cat sample;echo $?")
        >

        The problem is that echo outputs to the standard output and normally returns a
        true process exit status code (Unless there's a very unlikely event that it
        will fail outputting something to standard output.). And as an impedance
        mismatch, system() returns the status code, and does not trap the output.

        You can just do something like:

        <<<
        my $rc = system("cat sample");
        >>>

        And then look at $rc. I hope this is just for the sake of the example, because
        you should not use cat from Perl. (use perldoc -f open , perldoc -f system,
        etc.).

        Regards,

        Shlomi Fish

        > The file sample is not there in the directory which i am executing the
        > above command so it will through an error and i want to capture that error
        > and display it as echo $? but i am not able to do so.
        >
        > when i exeucte the above code echo $? is giving me 0 even the sample file
        > doesn't exist.
        >
        > Is there any way i can capture that error code in one system function call.

        --
        -----------------------------------------------------------------
        Shlomi Fish http://www.shlomifish.org/
        "Star Trek: We, the Living Dead" - http://shlom.in/st-wtld

        Deletionists delete Wikipedia articles that they consider lame.
        Chuck Norris deletes deletionists whom he considers lame.

        Please reply to list if it's a mailing list post - http://shlom.in/reply .
      Your message has been successfully submitted and would be delivered to recipients shortly.