如何使用Perl快速检查是否安装了Linux`unzip`?

时间:2009-12-10 16:13:54

标签: linux perl

如何使用Perl快速检查Linux unzip是否已安装?

11 个答案:

答案 0 :(得分:13)

跑吧。

严重。

据推测,为什么你想知道它是否已安装的原因是因为你需要稍后运行它。在这种情况下,还不足以知道它是否已安装 - 无论如何 - 您还需要知道它是否可执行,是否在路径中,脚本运行的用户ID是否具有运行所需的权限它,等等。您只需运行即可查看所有内容。

答案 1 :(得分:12)

`which unzip`

如果有输出,则指向解压缩位置。如果没有输出,则不会显示任何内容。这依赖于解压缩你的道路。

答案 2 :(得分:9)

这将验证您的路径上是否有unzip命令,以及当前用户是否可以执行此命令。

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

答案 3 :(得分:3)

取自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;
}

然后:

my $is_it_there = can_run("unzip");

答案 4 :(得分:3)

我只使用Archive::Extract并将其配置为更喜欢二进制文件到Perl模块。如果unzip存在,则使用它。否则,它会回归纯粹的Perl。

答案 5 :(得分:2)

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

答案 6 :(得分:1)

任何特定的unzip?我使用的Linux系统有Info-Zip的unzip,如果这是你要检查的,你可以做

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

如果您希望这更安全一点,您可以这样做:

#!/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
}

另见my multi-which script

答案 7 :(得分:1)

也许您应该退一步问为什么需要Perl的unzip命令。是因为你想要解压缩一些东西吗?如果是这样,那么您应该考虑使用可用的许多模块中的一个来以编程方式执行此操作,例如Archive::Zip

答案 8 :(得分:1)

为什么要在使用Perl时调用系统命令?使用解压缩模块,例如Archive::Extract,(或CPAN中的其他模块)

答案 9 :(得分:0)

答案 10 :(得分:0)

您可以尝试此脚本(已测试)。它利用了哪个命令。

#!/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);
}

干杯,

布拉德

相关问题