Reputation: 1390
I've written a script which scans a server for world-writable files. In the middle of the script is a bit of code which tests for the existence, non-existence, or zero size of a file.
# 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
}
If I remove the file being tested the $errHeader
variable is set appropriately per the does-not-exist test and, further down the script, is properly written to the output file. However, if I create the file and leave it empty the $errHeader
variable is not set properly.
I've reordered the tests putting the zero-size test above the does-not-exist test with the same result.
In another, simple test it works:
#!/usr/bin/perl
my $test;
if (-z "/usr/local/etc/world_writable_excludes.txt") {
$test = "Zero Size";
}
if ($test) {
print $test . "\n";
}
Upvotes: 0
Views: 610
Reputation: 2277
The problem is with -e
being your first condition. It is true even if your file has size 0. Try !-e
first, then -z
and then -e
.
Upvotes: 2