Harry: On Mon, Jan 05, 2015 at 06:20:06PM -0500, Harry Putnam wrote: > When running shell commands from perl script with an open() instead of > system() or exec how is the return value snagged. > > I guess, if it doesn't die, it worked but how to snag that information > and at what point? > > ------- 8< snip ---------- 8< snip ---------- 8<snip ------- > #!/usr/local/bin/perl > > use strict; > use warnings; > > my $log = '/home/harry/some.log'; > my $cmd = 'rsnapshot -c rsnap_Home.conf hourly'; > > open my $fh, '>', "$log" or die > "Can't open $log: $!"; > > open my $ch, '-|', "$cmd" or die > "Can't run $cmd: $!"; > > while (<$ch>) { > print; > print $fh; > } > > close $fh; > close $ch; > > __END__
You can check the exit status of a pipe after closing it. You should also be checking the return value of the close because it might indicate a problem closing the pipe which might affect the exit status, for example. perldoc -f close > If the filehandle came from a piped open, "close" returns false > if one of the other syscalls involved fails or if its program > exits with non-zero status. If the only problem was that the > program exited non-zero, $! will be set to 0. Closing a pipe > also waits for the process executing on the pipe to exit--in > case you wish to look at the output of the pipe afterwards--and > implicitly puts the exit status value of that command into $? > and "${^CHILD_ERROR_NATIVE}". So for example... unless (close $pipe) { my ($errno, $child_error) = ($!, $?); if ($errno == 0) { # Closing the pipe succeeded, but the exit status was # non-zero. my $signal = $child_error & 127; my $status = $child_error >> 8; handle_pipe_exit($signal, $status); } else { # One or more system calls failed when attempting to # close the pipe. $! should be set appropriately. I don't # know if you can rely on the process having exited/died # or on the exit status being set. handle_pipe_exit_error($errno); } }; Regards, -- Brandon McCaig <bamcc...@gmail.com> <bamcc...@castopulence.org> Castopulence Software <https://www.castopulence.org/> Blog <http://www.bamccaig.com/> perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'
signature.asc
Description: Digital signature