joseph wrote:
All,

Just like to ask for correction on what's wrong with my script it gives spit out this error when i run it.

Unsuccessful open on filename containing newline at disksize.pl line 8.
Can't open file No such file or directory

But it runs without this error whenever i feed it up when an existing file output by df-h.
ex:## open(FL,"path/toactual/file") or die "blabalha";
Does this mean it can't trap the output of  `df-h`?

Here's the script###

#!/usr/bin/perl

use strict;
use warnings;

chomp(my $output_file = `df -h`);


Df can return multiple lines. I'd suspect that your string $output_file
has several embedded newlines in it at this point. Chomp will only
remove that last (not embedded) newline.

open(FL,"$output_file") or die "Can't open file $!\n";
my @list;
my %disk;
my($label,$size,$use,$avail,$percent,$mounted,$partition,$usage);
while(<FL>) {
     ($label,$size,$use,$avail,$percent,$mounted) = split(/\s+/, $_);
     push @list,$mounted,$percent;

You might not need the intermediate list:
        $disk{$mounted} = $percent;

}

%disk =(@list);
delete $disk{"Mounted"};



foreach (sort keys %disk) {
       my $value = $disk{$_};
       print "\t $_ => $value \n";
}





I'd do it like this:

use strict;
use warnings;
use Data::Dumper;

my %disk;
for (`df -h`) {
    next if ! m{/};
    my($label,$size,$use,$avail,$percent,$mounted,$partition,$usage) =
        split /\s+/, $_;
    $disk{$mounted} = $percent;
}
print Dumper(\%disk);

__END__

You'll notice that the backticks operator (``), in a list context,
returns a list containing each line as a separate list element.

Since you only seem to need two elements from the list returned by
split(), you could change the two lines after the "m{/}" to this:

        my @list = split /\s+/, $_;
        $disk{$list[5]} = $list[4];

HTH




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to