I have to check the free space on a disk volume and am using the command df -h
, which produces this output:
Filesystem Size Used Avail Use% Mounted on
/dev/md0 27G 24G 2.1G 92% /
udev 506M 112K 506M 1% /dev
/dev/sda1 102M 40M 63M 39% /boot
This piece of script
my (@space, @freesp);
@space = grep /\/dev\/md0/,`df -h`;
print "@space\n";
is giving me
/dev/md0 27G 24G 2.1G 92% /
Which I can split on whitespace giving the values in $freesp[0]
, $freesp[1]
etc.
But I want to remove or replace that G
with white space, so that I can compare $freesp[3]
with some value and can proceed with my script.
So what is the regex I have to use for this to happen, or is there any other better way to do this?
This code is working for me, but I'm looking in a direction I have mentioned.
#!/usr/bin/perl
use strict;
use warnings;
my (@space, @freesp);
@space = grep /\/dev\/md0/, `df`;
print "@space\n";
for (@space) {
chomp;
@freesp = split /\s+/, $_;
}
if ($freesp[3]/1024/1024 < 2.0) {
print "Space is less\n";
}
else {
print "Space is OK\n";
}
#print ($freesp[3]/1024/1024);