I'm trying to help a friend write a simple Perl script that will test whether or not a file exists or not, and if it does, write the name of the file in a file called exists.txt. The files that don't exist go in nonexist.txt. The script reads the names of the files from a file that looks like this...
"/etc/samba/smb.conf"
"/etc/nonexistent file"
"/usr/doesnt_exist"
"/var/log/messages"
Now, my script works perfect, except the problem is that some of his files have trailing whitespace at the end of the filename.
How do you test if a file exists when it has trailing whitespace at the end?
Thanks,
Chris
"/etc/samba/smb.conf"
"/etc/nonexistent file"
"/usr/doesnt_exist"
"/var/log/messages"
Now, my script works perfect, except the problem is that some of his files have trailing whitespace at the end of the filename.
Code:
#!/usr/bin/perl -w
$a = '/root/testing/a.txt';
open(FILE, $a) or die "Can't open file: $!\n";
@lines = <FILE>;
close FILE;
$exists = '/root/testing/exists.txt';
open(EXISTS, ">$exists") or die "Couldn't open exists.txt for writing: $!\n";
$nonexist = '/root/testing/nonexist.txt';
open(NONEXIST, ">$nonexist") or die "Couldn't open nonexists.txt for writing: $!\n";
foreach (@lines) {
$_ =~ s/"//g;
chomp;
if (-e $_) {
print "$_ exists\n";
print EXISTS "$_\n";
} else {
print "$_ doesn't exist\n";
print NONEXIST "$_\n";
}
}
How do you test if a file exists when it has trailing whitespace at the end?
Thanks,
Chris