catch error code from system()

Discussion in 'Perl Misc' started by justme, Aug 10, 2004.

  1. justme

    justme Guest

    hi

    i wrote a perl script that is supposed to run a java program.

    my $cmd ="/usr/bin/java javaprog";
    system($cmd);

    the java prog returns an error code if there is an error when the java prog runs
    how can i catch the return code of the java prog reliably ?
    thanks
     
    justme, Aug 10, 2004
    #1
    1. Advertising

  2. justme

    Sam Holden Guest

    On 9 Aug 2004 19:30:40 -0700, justme <> wrote:
    > hi
    >
    > i wrote a perl script that is supposed to run a java program.
    >
    > my $cmd ="/usr/bin/java javaprog";
    > system($cmd);
    >
    > the java prog returns an error code if there is an error when the java prog runs
    > how can i catch the return code of the java prog reliably ?
    > thanks


    What does the documentation for the perl system function say?

    --
    Sam Holden
     
    Sam Holden, Aug 10, 2004
    #2
    1. Advertising

  3. justme

    Paul Lalli Guest

    On Mon, 9 Aug 2004, justme wrote:

    > hi
    >
    > i wrote a perl script that is supposed to run a java program.
    >
    > my $cmd ="/usr/bin/java javaprog";
    > system($cmd);
    >
    > the java prog returns an error code if there is an error when the java prog runs
    > how can i catch the return code of the java prog reliably ?
    > thanks


    Uhm, by not throwing away the return value from system?

    my $retval = system($cmd);

    (note that one more step is actually needed. Read
    perldoc -f system
    for more info)

    Paul Lalli
     
    Paul Lalli, Aug 10, 2004
    #3
  4. justme wrote:
    > i wrote a perl script that is supposed to run a java program.
    >
    > my $cmd ="/usr/bin/java javaprog";
    > system($cmd);
    >
    > the java prog returns an error code if there is an error when the
    > java prog runs how can i catch the return code of the java prog
    > reliably ? thanks


    Please provide a complete but minimal program that demonstrates that the
    method described in the documentation for system() doesn't work.
    Then people will sure be happy to investigate.

    jue
     
    Jürgen Exner, Aug 10, 2004
    #4
  5. justme <> wrote:

    > how can i catch the return code of the java prog reliably ?



    Please do not ask thousands of people to read the docs to you.


    perldoc -f system


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Aug 10, 2004
    #5
  6. justme

    J. Romano Guest

    (justme) wrote in message news:<>...
    >
    > i wrote a perl script that is supposed to run a java program.
    >
    > my $cmd ="/usr/bin/java javaprog";
    > system($cmd);
    >
    > the java prog returns an error code if there is an error when the java prog runs
    > how can i catch the return code of the java prog reliably ?


    There are several ways (read "perldoc -f system" to find out what
    some of them are). One of the simplest ways is to check the $?
    variable, like this:

    my $cmd ="/usr/bin/java javaprog";
    system($cmd);
    my $errorCode = $? >> 8; # don't forget the ">> 8" part!
    print "The error-code from the command was $errorCode.\n";

    Hope this helps. You might also want to read the "perldoc perlvar"
    documentation and search for the "$?" variable for more advice.

    -- J.
     
    J. Romano, Aug 10, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Black
    Replies:
    8
    Views:
    4,160
    Xenos
    Aug 20, 2004
  2. Replies:
    1
    Views:
    381
    Diez B. Roggisch
    Dec 12, 2005
  3. Adam
    Replies:
    9
    Views:
    576
    red floyd
    Feb 2, 2006
  4. rote
    Replies:
    4
    Views:
    1,734
    Alexey Smirnov
    Aug 4, 2008
  5. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    569
    Daniel Pitts
    Aug 5, 2009
Loading...

Share This Page