Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > why is return always 0?

Reply
Thread Tools

why is return always 0?

 
 
jammer
Guest
Posts: n/a
 
      03-17-2008
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";
}
 
Reply With Quote
 
 
 
 
sandy_saydakov@yahoo.com
Guest
Posts: n/a
 
      03-17-2008
On 16 Mar, 17:45, jammer <jamesloc...@mail.com> 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/
 
Reply With Quote
 
 
 
 
jammer
Guest
Posts: n/a
 
      03-17-2008
On Mar 16, 9:10 pm, sandy_sayda...@yahoo.com wrote:
> On 16 Mar, 17:45, jammer <jamesloc...@mail.com> 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.
 
Reply With Quote
 
sandy_saydakov@yahoo.com
Guest
Posts: n/a
 
      03-17-2008
On Mar 17, 12:56 pm, jammer <jamesloc...@mail.com> 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/
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-17-2008
jammer <> wrote:
> On Mar 16, 9:10 pm, sandy_sayda...@yahoo.com wrote:
> > On 16 Mar, 17:45, jammer <jamesloc...@mail.com> 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.
 
Reply With Quote
 
comp.llang.perl.moderated
Guest
Posts: n/a
 
      03-18-2008
On Mar 16, 5:45 pm, jammer <jamesloc...@mail.com> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
it always return path C:\winnt\system32 ..why ? raagz ASP .Net 2 07-16-2004 02:16 PM
it always return path C:\winnt\system32 ..why ? raagz ASP .Net 0 07-16-2004 01:47 PM
it always return path C:\winnt\system32 ..why ? raagz ASP .Net 0 07-16-2004 01:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57