Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: best practices for checking system return values for piped commands?

by eXile (Priest)
on May 02, 2006 at 02:41 UTC ( #546801=note: print w/ replies, xml ) Need Help??


in reply to Re: best practices for checking system return values for piped commands?
in thread best practices for checking system return values for piped commands?

I second the use of bash-es PIPESTATUS, I've used it in code like this:

my %cmds = ( 'bash' => '/usr/local/bin/bash',
             'gzip' => '/usr/bin/gzip',
             'lzop' => '/usr/bin/lzop',
             'wc'   => '/usr/bin/wc' );


my @zip_results = `$cmds{'bash'} -c '$cmds{$compress} -dc $file | $cmds{'wc'} -c ; echo \${PIPESTATUS\@}'`;
The last line of @zip_results has the status of both the 'compress' command and the 'wc' command.


Comment on Re^2: best practices for checking system return values for piped commands?
Re^3: best practices for checking system return values for piped commands?
by Anonymous Monk on Apr 08, 2009 at 07:33 UTC
    I'm struggling with trying to identify a failure in a set of piped commands run on a remote host via ssh. I use perl to build and run the command line as below
    ssh $TARGET_HOST "sudo find $TARGET_DIR -name \'$TARGET_FILE\' | sudo +cpio --create --format ustar" | bzip2 -9 -c
    Initially I just redirected the output to a file, which is fine if both the $TARGET_DIR and $TARGET_FILE exists. However, if one or both do not exist, then I still get a file created, but it's empty.
    Things I've tried:
    With $ret=system($cmd); and testing the the value of $ret:
  • Pre-fixing the cmd with set -o pipefail && .
  • Adding set -o pipefail to the command run by ssh.
  • Adding set -o pipefail to the command run by sudo.
  • Doing the bzip2 locally.
    With @results = `$cmd`, and testing the value of $?:
  • Adding ; echo \${PIPESTATUS\@}; to the end of $cmd, within the string passed to ssh.
  • Adding ; echo \${PIPESTATUS\@}; to the end of $cmd, after the string passed to ssh.
    Using the @results approach, I only get one entry, so I suspect that the backtick method only sees one stream?

    Any ideas on how I can detect the remote pipeline failure?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://546801]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (13)
As of 2014-12-22 14:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (119 votes), past polls