When the script below executes the command, it does so on another host by ssh'ing (or rsh'ing) to that box. I need to capture the return code of the script. I have tried using pipes but I always get a successful $? because the ssh/rsh is always successful. Let me show you:
---- test.sh --- #!/bin/sh echo "Hello world" exit 1 <-- Note that we are forcibly exiting non-zero ---- runme.pl ---- #!/usr/bin/perl local $SIG{CHLD} = sub { wait; } # Shell to the "hosta" and run the script. # Since it will fail, return the word FAIL my $cmd = "ssh hosta -n \"/tmp/test.sh || echo FAIL\"; my ($read, $write, $pid) = spawnMe($cmd); close ( $write ); # Not doing any writes while ( <$read> ) { my $buf = 'X' x 512; if ( my $nread = sysread( $read, $buf, 512 ) ) { my @l = split /\r?\n/, $buffer . $buf; if ($buf !~ /\n$/s ) { $buffer = pop @l; } else { $buffer = ''; } map { print "$_\n"; } @l; } } my $kid = waitpid($pid, WNOHANG); unless ($kid == $pid) { close ($read); $return_code = $?; } else { print "Reaped status: $?" $return_code = $?; }
# I always get an exit status of "0" because the ssh was successful. How do I get the "FAIL" ? Help.

In reply to Pipe and return status through an "ssh" by sdolancu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.