I'm trying to have the script check if a file has data or not, and then process it accordingly. If the file is empty, I want it to return "nothing to do", if not, I want it to process the file line by line. This is what I have so far, but it always returns "nothing to do", even if the file is not empty. Any ideas?
system "rm file2";
open(FILE2, ">file2");
open(MYINPUTFILE, "file");
if ( ! -s file ){
print file2 "Nothing to do\n";
} else {
while(<MYINPUTFILE>) {
my($line) = $_;
chomp($line);
print file2 "$line\n";
print file2 "you need to do this\n";
print file2 " \n";
}
}
Would it not just be
if ( -s file ){
as the -s means if the file is not empty.. would using the ! not cancel that out?
this is what man page says.....
-s FileName
Returns a True exit value if the specified FileName exists and has a size greater than 0.
You are correct, however, I already attempted that and swapped my "if" and "else" conditions, but I always got an error about the "else" line having a syntax error, see code and errors below.
if ( -s file ) {
while(<MYINPUTFILE>) {
my($line) = $_;
chomp($line);
print FILE2 "$line\n";
print FILE2 "You need to do this\n";
print FILE2 " \n";
}
else
{
print FILE2 "Nothing to be done\n";
}
}
syntax error at ansipre3 line 51, near "else"
syntax error at ansipre3 line 55, near "}"
how about
using :
instead of :
don't know much about perl , it worked for me.