Perl:零大小(-z)的文件测试未按预期执行

时间:2014-05-21 03:59:26

标签: perl

我编写了一个脚本,用于扫描服务器以查找世界可写文件。在脚本的中间是一些代码,用于测试文件的存在,不存在或零大小。

# Read in the list of excluded files and create a regex from them
if (-e $exclude) {
  $regExcld = do {
    open XCLD, "<${exclude}" or die "Cannot open ${exclude}, $!\n";
    my @ignore = <XCLD>;
    chomp @ignore;
    local $" = '|';
    qr/@ignore/;
  };

} elsif ((! -e $exclude) || (-z $exclude)) {
  $errHeader = <<HEADER;
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!                                                  !!
!! /usr/local/etc/world_writable_excludes.txt is    !!
!! is missing or empty. This report includes        !!
!! every world-writable file including those which  !!
!! are expected and should be excluded.             !!
!!                                                  !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


HEADER

}

如果删除正在测试的文件,则根据不存在的测试正确设置$errHeader变量,并在脚本的下方正确写入输出文件。但是,如果我创建文件并将其留空,则$errHeader变量 设置不正确。

我已经重新排序测试,将零大小的测试置于不存在的测试之上,结果相同。

在另一个简单的测试中它起作用:

#!/usr/bin/perl

my $test;

if (-z "/usr/local/etc/world_writable_excludes.txt") {
  $test = "Zero Size";

}

if ($test) {
  print $test . "\n";

}

1 个答案:

答案 0 :(得分:2)

问题在于-e是你的第一个条件。即使您的文件大小为0,也是如此。首先尝试!-e,然后-z然后-e