#!/usr/bin/perl use strict; use warnings; my @headers = qw(name size used free capacity mount); my @df = &readDFfile('test.dat'); shift @df; # get rid of the header my %devices; for my $line (@df) { my %info; @info{@headers} = split /\s+/, $line; # note the hash slice $info{capacity} = _percentage_to_decimal($info{capacity}); if (!defined $info{mount}) { my $displayLine = $line; chomp $displayLine; print "\$info{mount} undefined when \$line = [$displayLine]\n"; } else { $devices{ $info{mount} } = \%info; } } # Change 12.3% to .123 sub _percentage_to_decimal { my $percentage = shift; if (!defined $percentage) { $percentage = 0; } $percentage =~ s{%}{}; return $percentage / 100; } # Now the information for each device is in a hash of hashes. # Show how much space is free in device /dev/ad4s1e print $devices{"/production/log"}{free} ; print "\n"; for my $info (values %devices) { # Skip to the next device if its capacity is not over 60%. next unless $info->{capacity} > .10; # Print some info about each device printf "%s is at %d%% with %dK remaining.\n", $info->{mount}, $info->{capacity}*100, $info->{free}; } exit; sub readDFfile { my ($rdofnm, @arglst) = @_; if (!defined $rdofnm) { $rdofnm = ''; } # -----[ Was command, now reading from file for testing purposes ]-------------- # my @df = `df -k`; # ------------------------------------------------------------------------------ my @df = (); if (open RDOFIL, '<', $rdofnm) { @df = ; close RDOFIL; } # ------------------------------------------------------------------------------ return @df; } __END__