[ACCEPTED]-How can I quickly check if Linux `unzip` is installed using Perl?-perl

Accepted answer
Score: 13
`which unzip`

If there's output, it points to unzip's 3 location. If there isn't output, then nothing 2 will show up. This relies on unzip being 1 on your path.

Score: 13

Just run it.

Seriously.

Presumably, the reason 9 why you want to know if it is installed, is 8 because you need to run it later. In that 7 case, it isn't enough to know whether it 6 is installed, anyway – you also 5 need to know whether it is executable, whether 4 it is in the path, whether the user id the 3 script is running under has the necessary 2 privileges to run it, and so on. You can 1 check all that by simply just running it.

Score: 9

This verifies if unzip command is on your path 2 and also if it is executable by current 1 user.

print "unzip installed" if grep { -x "$_/unzip"}split /:/,$ENV{PATH}
Score: 3

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");

0

Score: 3

I just use Archive::Extract and configure it to prefer binaries 2 to Perl modules. If unzip is there, it uses it. Otherwise, it 1 falls back to pure Perl.

Score: 2
perl -e 'if (-e "/usr/bin/unzip") { print "present\n"; } else { print "not present\n"; }'

0

Score: 1

Any particular unzip? Linux systems I use have 4 Info-Zip's unzip and if that is what you want 3 to check for, you can do

if ( (`unzip`)[0] =~ /^UnZip/ ) {
# ...
}

If you want this 2 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 1 my multi-which script.

Score: 1

Perhaps you should step back and ask why 4 you need the unzip command from Perl. Is it because 3 you want to unzip something? If so, then 2 you should consider doing this programmatically 1 with one of the many modules available, e.g. Archive::Zip.

Score: 1

why do you want to call system command when 2 using Perl.? use an unzip module such as 1 Archive::Extract, (or others in CPAN)

Score: 0
Score: 0

You could try this script (Tested). It utilizes 1 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

More Related questions