I need some help tweaking my code to look for another attribute in this unix df output:
Ex.
Filesystem Size Used Avail Capacity Mounted on
/dev/ad4s1e 61G 46G 9.7G 83% /home
So far I can extract capacity, but now I want to add Avail.
Here is my perl line that grabs capacity. How do I get “Avail”?? Thanks!
my @df = qx (df -k /tmp);
my $cap;
foreach my $df (@df)
{
($cap) =($df =~ m!(\d+)\%!);
};
print "$cap\n";
This has the merit of producing a nice data structure for you to query all the info about each filesystem.
Now the information for each device is in a hash of hashes.
This isn’t the simplest way to do it, but it is the most generally useful way to work with the df information putting it all in one nice data structure that you can pass around as needed. This is better than slicing it all up into individual variables and its a technique you should get used to.
UPDATE:
To get all the devices which have >60% capacity, you’d iterate through all the values in the hash and select those with a capacity greater than 60%. Except capacity is stored as a string like “88%” and that’s not useful for comparison. We could strip out the % here, but then we’d be doing that everywhere we want to use it. Its better to normalize your data up front, that makes it easier to work with. Storing formatted data is a red flag. So I’ve modified the code above which reads from
dfto change the capacity from 88% to .88.Now its easier to work with.
I chose to use printf here rather than interpolation because it makes it a bit easier to see what the string will look like when output.