getting the return value from a piped command

C

carlisle411

I've mocked up a simplified version of my problem. In it, I'm trying
to determine the result of my tar command - was it successful?. I have
to use the "command |" syntax to invoke the command. All my normal
channels for finding the solution have come up dry. Thoughts? Thanks.

-j

--------------------------------------
$ ./test.pl
tar: filethatdoesnotexist: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors
TAR was successful
$


-----------test.pl---------------------------
#!/usr/bin/perl
use strict;

my $command = "tar -czv -f whatever.tar.gz filethatdoesnotexist";

eval
{
open COMMAND, "$command|";
while (<COMMAND>)
{
print;
}
close COMMAND;
};
if ( $@ )
{
print "!! TAR Failed !!\n";
return -1;
}
else
{
print "TAR was successful\n";
}
 
U

usenet

Thoughts?

Yeah, FWIW, try it in Perl instead of using Perl as a glorified shell
scripting language. Running shell commands from Perl can invite many
problems - for example, what happens if your filename contains
whitespaces? You need to escape all that, and on and on and on.

CPAN is your friend for robust, cross-platform, reliable, and Perl-ish
alternatives to system commands and shell scripts. The module will die
with an appropriate error message if the $tar object cannot be created
- do it in an eval() if you prefer to do your own exception handling:

#!/usr/bin/perl
use strict; use warnings;
use Archive::Tar;

my $file = '/tmp/no_such_file.tar.gz'; # invalid file (for test)
#my $file = '/tmp/junk.tar.gz'; # valid file (for test)

my $compressed = 1;

my $tar = Archive::Tar -> new($file, $compressed);

#What is in this thing anyway?
print map {"$_\n"} $tar -> list_files();

__END__
 
A

A. Sinan Unur

(e-mail address removed) wrote in
I've mocked up a simplified version of my problem. In it, I'm trying
to determine the result of my tar command - was it successful?. I
have to use the "command |" syntax to invoke the command. All my
normal channels for finding the solution have come up dry. Thoughts?

Here's what perldoc perlopentut has to say about this:

What happens if you try to open a pipe to or from a non-existent
command? If possible, Perl will detect the failure and set $! as usual.
But if the command contains special shell characters, such as ">" or
"*", called 'metacharacters', Perl does not execute the command
directly. Instead, Perl runs the shell, which then tries to run the
command. This means that it's the shell that gets the error indication.
In such a case, the "open" call will only indicate failure if Perl
can't even run the shell. See "How can I capture STDERR from an
external command?" in perlfaq8 to see how to cope with this. There's
also an explanation in perlipc.

This, in conjunction with

perldoc -q stderr

should get you started.

Basically, the idea is to examine what tar prints to its STDERR to
decide if it succeeded.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top