How can I quickly check if Linux unzip
is installed using Perl?
views:
268answers:
11perl -e 'if (-e "/usr/bin/unzip") { print "present\n"; } else { print "not present\n"; }'
`which unzip`
If there's output, it points to unzip's location. If there isn't output, then nothing will show up. This relies on unzip being on your path.
This verifies if unzip
command is on your path and also if it is executable by current user.
print "unzip installed" if grep { -x "$_/unzip"}split /:/,$ENV{PATH}
Any particular unzip
? Linux systems I use have Info-Zip's unzip
and if that is what you want to check for, you can do
if ( (`unzip`)[0] =~ /^UnZip/ ) {
# ...
}
If you want this to be a little safer, you would do:
#!/usr/bin/perl -T
use strict; use warnings;
$ENV{PATH} = '/bin:/usr/bin:/usr/local/bin';
use File::Spec::Functions qw( catfile path );
my @unzip_paths;
for my $dir ( path ) {
my $fn = catfile $dir, 'unzip';
push @unzip_paths, $fn if -e $fn;
}
if ( @unzip_paths > 1 ) {
# further narrow down by version etc
}
See also my multi-which script.
Taken from Module::Install::Can:
sub can_run {
my ($self, $cmd) = @_;
my $_cmd = $cmd;
return $_cmd if (-x $_cmd or $_cmd = MM->maybe_command($_cmd));
for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') {
next if $dir eq '';
my $abs = File::Spec->catfile($dir, $_[1]);
return $abs if (-x $abs or $abs = MM->maybe_command($abs));
}
return;
}
Then:
my $is_it_there = can_run("unzip");
Perhaps you should step back and ask why you need the unzip
command from Perl. Is it because you want to unzip something? If so, then you should consider doing this programmatically with one of the many modules available, e.g. Archive::Zip.
I just use Archive::Extract and configure it to prefer binaries to Perl modules. If unzip
is there, it uses it. Otherwise, it falls back to pure Perl.
why do you want to call system command when using Perl.? use an unzip module such as Archive::Extract, (or others in CPAN)
You could try this script (Tested). It utilizes the which command.
#!/usr/bin/perl -w
use strict;
my $bin = "unzip";
my $search = `which $bin 2>&1`;
chomp($search);
if ($search =~ /^which: no/)
{
print "Could not locate '" . $bin . "'\n";
exit(1);
} else {
print "Found " . $bin . " in " . $search . "\n";
exit(0);
}
Cheers,
Brad