![](/img/trans.png)
[英]How can I check if PostgreSQL is installed or not via Linux script?
[英]How can I quickly check if Linux `unzip` is installed using Perl?
如何快速檢查是否使用Perl安裝Linux unzip
?
跑吧。
認真。
據推測, 為什么你想知道如果安裝它的原因,是因為你以后需要運行它。 在這種情況下,還不足以知道它是否已安裝 - 無論如何 - 您還需要知道它是否可執行,是否在路徑中,腳本運行的用戶ID是否具有運行所需的權限它,等等。 您只需運行即可查看所有內容。
`which unzip`
如果有輸出,則指向解壓縮的位置。 如果沒有輸出,則不會顯示任何內容。 這依賴於解壓縮你的道路。
這將驗證unzip
命令是否在您的路徑上以及當前用戶是否可執行。
print "unzip installed" if grep { -x "$_/unzip"}split /:/,$ENV{PATH}
取自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");
我只是使用Archive :: Extract並將其配置為更喜歡二進制文件到Perl模塊。 如果unzip
,它會使用它。 否則,它會回歸純粹的Perl。
perl -e 'if (-e "/usr/bin/unzip") { print "present\n"; } else { print "not present\n"; }'
任何特定的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
}
另請參閱我的多個腳本 。
也許你應該退一步問為什么你需要Perl的unzip
命令。 是因為你想要解壓縮一些東西嗎? 如果是這樣,那么你應該考慮使用眾多可用模塊之一進行編程,例如Archive :: Zip 。
為什么要在使用Perl時調用系統命令? 使用解壓縮模塊,例如Archive :: Extract ,(或CPAN中的其他模塊)
你可以嘗試這個腳本(測試)。 它利用了which命令。
#!/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);
}
干杯,
布拉德
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.