[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[ale] existence of a file



Terry Bailey wrote:
> How do you test if a file does not exist?
>
> I thought it would be
>
> 	if (!(-e $file)
>
> but that doesn't seem to be working.
>   
Define "doesn't seem to be working" (and also what you're using; it 
looks like Perl, but . . .).

$ mkdir foozle
$ cd ./foozle
$ ls
$ touch exists
$ ls
exists
$ perl -le 'for my $file ( qw( exists nonexistent ) ) { print "$file 
exists" if -e $file }'
exists exists
$ perl -le 'for my $file ( qw( exists nonexistent ) ) { print "$file 
does not exist" if !-e $file }'
nonexistent does not exist
$


And keep in mind that explicitly testing for a files existence with -e 
can create a race condition that could lead to problems down the line.