why is return always 0?

Discussion in 'Perl Misc' started by jammer, Mar 17, 2008.

  1. jammer

    jammer Guest

    How do I make tftp fail if the host is bad?

    my $rc = system( "/usr/bin/tftp bad$host < $tempFile 2>&1 >/
    dev/null" );
    if ( $rc != 0 ) {
    print "unable to tftp to '$host'\n";
    exit 1;
    } else {
    print "success: $rc\n";
    }
     
    jammer, Mar 17, 2008
    #1
    1. Advertising

  2. jammer

    Guest

    On 16 Mar, 17:45, jammer <> wrote:
    > How do I make tftp fail if the host is bad?
    >
    > my $rc = system( "/usr/bin/tftp bad$host < $tempFile 2>&1 >/dev/null" );
    > if ( $rc != 0 ) {
    > print "unable to tftp to '$host'\n";
    > exit 1;
    > } else {
    > print "success: $rc\n";
    > }


    Make sure the binary returns non-zero on failure. Try that command on
    the command line and do "echo $?" right after.

    -sandy
    http://myperlquiz.com/
     
    , Mar 17, 2008
    #2
    1. Advertising

  3. jammer

    jammer Guest

    On Mar 16, 9:10 pm, wrote:
    > On 16 Mar, 17:45, jammer <> wrote:
    >
    > > How do I make tftp fail if the host is bad?

    >
    > > my $rc = system( "/usr/bin/tftp bad$host < $tempFile 2>&1 >/dev/null" );
    > > if ( $rc != 0 ) {
    > > print "unable to tftp to '$host'\n";
    > > exit 1;
    > > } else {
    > > print "success: $rc\n";
    > > }

    >
    > Make sure the binary returns non-zero on failure. Try that command on
    > the command line and do "echo $?" right after.
    >
    > -sandyhttp://myperlquiz.com/


    It prints an error when run interactively.
    tftp does run, it just can't find the host.
    It returns 0 because it ran, didn't work but it ran. :-(
     
    jammer, Mar 17, 2008
    #3
  4. jammer

    Guest

    On Mar 17, 12:56 pm, jammer <> wrote:
    > > Make sure the binary returns non-zero on failure. Try that command on
    > > the command line and do "echo $?" right after.

    >
    > It prints an error when run interactively.
    > tftp does run, it just can't find the host.
    > It returns 0 because it ran, didn't work but it ran. :-(


    So there must be a bug in tftp then. It makes no sense to have a clean
    exit code on failure.

    For example, try it with 'ls':
    > ls nonexistent

    ls: nonexistent: No such file or directory
    > echo $?

    1
    This makes perfect sense, doesn't it?

    I would suggest trying a native TFTP Perl module:
    http://search.cpan.org/~gsm/TFTP-1.0b3/TFTP.pm

    /sandy
    http://myperlquiz.com/
     
    , Mar 17, 2008
    #4
  5. jammer

    Guest

    jammer <> wrote:
    > On Mar 16, 9:10 pm, wrote:
    > > On 16 Mar, 17:45, jammer <> wrote:
    > >
    > > > How do I make tftp fail if the host is bad?


    It seems that that is a question for the authors of tftp, not
    a Perl question. But there may be a Perl work-a-round.

    > >
    > > > my $rc = system( "/usr/bin/tftp bad$host < $tempFile 2>&1
    > > > >/dev/null" ); if ( $rc != 0 ) {
    > > > print "unable to tftp to '$host'\n";
    > > > exit 1;
    > > > } else {
    > > > print "success: $rc\n";
    > > > }

    > >
    > > Make sure the binary returns non-zero on failure. Try that command on
    > > the command line and do "echo $?" right after.
    > >
    > > -sandyhttp://myperlquiz.com/

    >
    > It prints an error when run interactively.


    Does it do that when run noninteractively? You could try capturing and
    parsing its stderr. IPC::Run might be a good way. (I think it might also
    help you trick tftp into thinking it is running interactively, if that is
    necessary.)

    > tftp does run, it just can't find the host.
    > It returns 0 because it ran, didn't work but it ran. :-(


    Well, that was probably a poor design decision. By that criterion, nothing
    should ever exit with anything but 0. If it ran, it ran. If it didn't
    run, it can't exit with anything at all.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
     
    , Mar 17, 2008
    #5
  6. On Mar 16, 5:45 pm, jammer <> wrote:
    > How do I make tftp fail if the host is bad?
    >
    > my $rc = system( "/usr/bin/tftp bad$host < $tempFile 2>&1 >/
    > dev/null" );
    > if ( $rc != 0 ) {
    > print "unable to tftp to '$host'\n";
    > exit 1;
    > } else {
    > print "success: $rc\n";
    > }


    On Solaris anyway, tftp doesn't return an exit code - just writes to
    stdout and then prompts.
    So you'd have to short-circuit its prompt and
    parse the output for specific errors, eg.:

    $out = qx { /usr/bin/tftp badhost </dev/null };
    if ( $? ) {
    die "can't start tftp: $?";
    } elsif ($out =~ /unknown host/ ) {
    die "failed: $out";
    } else {
    # ok
    ...
    }

    hth,
    --
    Charles DeRykus
     
    comp.llang.perl.moderated, Mar 18, 2008
    #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. raagz
    Replies:
    2
    Views:
    422
  2. raagz
    Replies:
    0
    Views:
    322
    raagz
    Jul 16, 2004
  3. raagz
    Replies:
    0
    Views:
    321
    raagz
    Jul 16, 2004
  4. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    922
    Mark Rae
    Dec 21, 2006
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,077
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page