4

How can I quickly check if Linux unzip is installed using Perl?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
user105033
  • 18,800
  • 19
  • 58
  • 69

11 Answers11

14
`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.

Zach
  • 2,145
  • 3
  • 17
  • 32
  • im assuming which is standard on all linux systems? – user105033 Dec 10 '09 at 16:35
  • It's been on every Unix and Linux system that I've used. – Christopher Bottoms Dec 10 '09 at 16:39
  • 1
    also, if it is not found, it returns a non-zero exit code (1 on Ubuntu 8.04) while if it is found it returns 0 – HalfBrian Dec 10 '09 at 16:49
  • 1
    `which` is neither part of POSIX, nor the Single Unix Specification, not the Linux Standard Base Specification. It's not installed on at least one of the Linux systems I'm using right now. I can remember several other Linux systems I have used in the past that didn't have it. – Jörg W Mittag Dec 10 '09 at 16:53
  • `which which` will tell you if `which` is installed :) On most modern systems, yes. On systems 2007 or earlier, its touch and go. `which` only searches your path anyway .. so a portable work around is to do that yourself. – Tim Post Dec 10 '09 at 16:59
  • `command -v` was added to both the Single Unix Specification and the Linux Standard Base Specification as a replacement for `which`. – Jörg W Mittag Dec 10 '09 at 17:00
  • okay so best bet would be to do a `unzip -v` and check for non 'command not found' output? – user105033 Dec 10 '09 at 17:13
  • This won't work if unzip is installed in a non $PATH location, for example "/usr/local/unzip", so it's not recommended. Also, on Solaris if not found, it will still return exit code 0. – Anders Jan 20 '10 at 19:49
  • Usage of which is discouraged, see http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script – TimZaman Jul 22 '15 at 12:28
13

Just run it.

Seriously.

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

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • May be the user wants to check for dependencies for his code and direct users to install them. – BioDeveloper Sep 12 '16 at 12:39
  • One might also be operating in an environment (like a remote web server) over which one has incomplete control and wish to check whether it is worthwhile uploading a zip or not. – DaveP Jun 29 '22 at 10:43
9

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}
catwalk
  • 6,340
  • 25
  • 16
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");
tsee
  • 5,034
  • 1
  • 19
  • 27
3

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.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
2
perl -e 'if (-e "/usr/bin/unzip") { print "present\n"; } else { print "not present\n"; }'
tsee
  • 5,034
  • 1
  • 19
  • 27
Brian Showalter
  • 4,321
  • 2
  • 26
  • 29
1

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.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
1

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.

Ether
  • 53,118
  • 13
  • 86
  • 159
  • 1
    Sometimes, you want to unzip stuff quickly. Then, Archive::Zip is the wrong choice. – tsee Dec 10 '09 at 17:07
1

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

ghostdog74
  • 327,991
  • 56
  • 259
  • 343
0

File::Which

hillu
  • 9,423
  • 4
  • 26
  • 30
0

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

user198470
  • 351
  • 3
  • 11