#!/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}); $devices{ $info{mount} } = \%info; } # Change 12.3% to .123 sub _percentage_to_decimal { my $percentage = shift; $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__ C:\Steve\Dev\PerlMonks\P-2013-12-18@1604-Hash-Undef>perl -v This is perl 5, version 16, subversion 3 (v5.16.3) built for MSWin32-x64-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2012, Larry Wall Binary build 1603 [296746] provided by ActiveState http://www.ActiveState.com Built Mar 13 2013 13:31:10 Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl". If you have access to the Internet, point your browser at http://www.perl.org/, the Perl Home Page. C:\Steve\Dev\PerlMonks\P-2013-12-18@1604-Hash-Undef>type test.dat Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/VolGroup00-LogVol00 28313732 9816924 17035356 37% / /dev/sda1 101086 27516 68351 29% /boot tmpfs 2987896 0 2987896 0% /dev/shm /dev/mapper/VolGroupPROD-ExportHome 15481840 3495504 11199904 24% /export/home /dev/mapper/VolGroupPROD-Production 15481840 1523692 13171716 11% /production /dev/mapper/VolGroupPROD-ProdLog 30963708 20410952 8979892 70% /production/log /dev/mapper/VolGroupPROD-ProdArchive 10313016 1693640 8095500 18% /production/archive C:\Steve\Dev\PerlMonks\P-2013-12-18@1604-Hash-Undef>perl testdf2.pl Use of uninitialized value $percentage in substitution (s///) at testdf2.pl line 20. Use of uninitialized value $percentage in division (/) at testdf2.pl line 21. Use of uninitialized value $info{"mount"} in hash element at testdf2.pl line 14. Use of uninitialized value $percentage in substitution (s///) at testdf2.pl line 20. Use of uninitialized value $percentage in division (/) at testdf2.pl line 21. Use of uninitialized value $info{"mount"} in hash element at testdf2.pl line 14. Use of uninitialized value $percentage in substitution (s///) at testdf2.pl line 20. Use of uninitialized value $percentage in division (/) at testdf2.pl line 21. Use of uninitialized value $info{"mount"} in hash element at testdf2.pl line 14. Use of uninitialized value $percentage in substitution (s///) at testdf2.pl line 20. Use of uninitialized value $percentage in division (/) at testdf2.pl line 21. Use of uninitialized value $info{"mount"} in hash element at testdf2.pl line 14. Use of uninitialized value $percentage in substitution (s///) at testdf2.pl line 20. Use of uninitialized value $percentage in division (/) at testdf2.pl line 21. Use of uninitialized value $info{"mount"} in hash element at testdf2.pl line 14. 8979892 /production/log is at 70% with 8979892K remaining. / is at 37% with 17035356K remaining. /boot is at 28% with 68351K remaining. /export/home is at 24% with 11199904K remaining. /production is at 11% with 13171716K remaining. /production/archive is at 18% with 8095500K remaining.