Just a small comment, I usually use backticks when I am interested in the output
of a command writing to stdout. If it does not then I think it's more
appropriate to use simply system. If you don't want stderr then you can close
stderr or redirect it to /dev/null (supposing unixlike here), this would be
"2>&-" o "2>/dev/null".
Supposing a program writes to stderr only error conditions
(and it should) seldom there is need to capture both stderr and stdout.
I usually do it when there is some problem and I want to grep everything
" cmd ... 2>&1 | grep ". Another case is when you want to capture the exit
status of an intermediate command in a shell pipeline, while normally you would
get the exit status of the last one (modern shell have a "pipefail" option --
and more-- to help that)
% stephan@ape (/home/stephan/t0) %
% echo 'just another perl hacker' > secret.txt
% stephan@ape (/home/stephan/t0) %
% zip -P sesame safe.zip secret.txt
adding: secret.txt (stored 0%)
% stephan@ape (/home/stephan/t0) %
% perl safe_unzip.px sesame safe.zip
.cmd: [unzip -qoP sesame safe.zip 2>/dev/null]
.okay: got secret file! status = [0]
1 just another perl hacker$
#!/usr/bin/perl
use strict;
use warnings;
my $password = shift or die "args?";
my $zipfile = shift or die "zipfile?";
my $secret_file = 'secret.txt';
# use -q (silent mode)
# use -o (overwrite mode) o unzip hangs if secret file already exists
my $cmd = "unzip -qoP $password $zipfile 2>/dev/null";
print ".cmd: [$cmd]\n";
system($cmd);
my $status = ($? >> 8); # status of cmd, like POSIX WIFEXITED() mac
+ro
if ($status) {
die "**ERROR: unzip failed! status = [$status]";
}
print ".okay: got secret file! status = [$status]\n";
-f $secret_file && print qx(cat -evnt $secret_file), "\n";
note that unzip (at least on my cygwin) returns 9 if it cannot find
the zip file, and the program above gets that code correctly:
% stephan@ape (/home/stephan/t0) %
% perl safe_unzip.px sesame safe1.zip
.cmd: [unzip -qoP sesame safe1.zip 2>/dev/null]
**ERROR: unzip failed! status = [9] at safe_unzip.px line 18.
hth
--stephan
|