Grace
Grace

Reputation: 440

File length is not 0 size but is empty, how to eliminate this?

I wrote a Perl wrapper applying some unix commands, writing the output to a log file. However I'm seeing it create some empty file but I'm seeing some of the files being created are not 0 size, it is stating 1byte instead; therefore I'm having problem on using the "if (-s "file") do something else do other thing" for some post processing stuff. Can somebody help why my code is creating an empty line (I guess, though I already place a chomp there), is there a way to eliminate that empty line so that the output log file will always go to 0 byte if there is no data being captured after processing the unix command? Or else.. can you suggest an effective way to check if file is not empty for case like this.

sub sub_unaccessfiles {
    my ($disk) = @_ ;
    @disk_sorted = split (/\s+/, $disk) ;
    $disk_Name = $disk_sorted[4] ;
    my $aging_disk = basename ($disk_Name) ;
    open (OUT3, ">./diskmonitor_logs/char_disks/aging_files_$aging_disk") || die "Cannot create ./diskmonitor_logs/char_disks/aging_files_$aging_disk: $!\n" ;
    my $cmd_aging_grab = `find $disk_Name -type f -name .snapshot -prune -o -atime +180 -printf '%u\t%s\t%t\t%p\n'` ;  
    chomp $cmd_aging_grab ;
    print OUT3 "$cmd_aging_grab\n" ;
    close OUT3 ;
       ..
       ..
}

Upvotes: 0

Views: 233

Answers (3)

shawnhcorey
shawnhcorey

Reputation: 3601

Try:

print OUT3 "$cmd_aging_grab\n" if $cmd_aging_grab =~ /\S/;

Upvotes: 0

pirhac
pirhac

Reputation: 927

Your print line:

print OUT3 "$cmd_aging_grab\n" ;

has a '\n' in it - causing the blank line everytime - even if the content of $cmd_aging_grab is empty

Upvotes: 5

Toto
Toto

Reputation: 91488

Remove the \n when writting to output file.

Upvotes: 1

Related Questions