joseph wrote:
> All,
Hello,
> 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 -h` does not return a file name, it returns the same output as if you had
run the command 'df -h' on the command line.
> 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>) {
There are two basic ways to do what you want:
1. for ( `df -h` ) { ... }
2. open FL, 'df -h |' or die "Cannot open df pipe: $!";
while ( <FL> ) { ... }
close FL or warn $! ? "Error closing df pipe: $!"
: "Exit status $? from df";
> ($label,$size,$use,$avail,$percent,$mounted) = split(/\s+/, $_);
You are only using two of the values so you can simplify that to:
my ( $percent, $mounted ) = ( split )[ -2, -1 ];
> push @list,$mounted,$percent;
You don't need the array, you can assign directly to the hash:
$disk{ $mounted } = $percent unless $mounted eq 'Mounted';
> }
>
> %disk =(@list);
> delete $disk{"Mounted"};
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>