Hi,
I have a little bash diddy to show available space on a hard drive ( not partitioned space ).
Being inexperienced in perl, I sure would like some ( friendly ) input.
Here is the bash script
Code:
#!/bin/bash
##### This script calculates hard drive space.
##### example: ./drive /dev/hda
usage()
{
echo "Example: $0 /dev/hda"
exit 1
}
test "$1" || usage
if ! [ -e $1 ]; then
echo "$1 is not a valid device!"
exit 1
fi
echo
drive=$(echo $1 | cut -d/ -f3)
drive_size=$(cat /proc/partitions | grep -e "$drive$" | awk '{print$3}')
drive_size=$(($drive_size / 1000))
IFS=$'\n'
for line in $(cat /proc/partitions | grep -e "$drive[0-9]") ; do
part=$(echo $line | awk '{print"/dev/"$4}')
size=$(echo $line | awk '{print$3}')
if [ $size = 1 ] ; then
echo "Partition $part -- Extended --"
else
size=$(($size / 1000))
echo "Partition $part used $size MB"
totalused=$(($totalused + $size))
fi
done
echo
free=$(($drive_size - $totalused))
#
echo -e "Total drive size $drive_size MB"
echo -e "Partitioned size $totalused MB"
echo -e "Unpartitioned size $free MB"
echo
echo
Here is what I have so far in the perl script.
Code:
#!/usr/bin/perl -w
##### This script calculates hard drive space.
##### example: ./drive /dev/hda
if($#ARGV != 0){
die "Example: $0 /dev/sda \n";
}
if(!-e $ARGV[0]){
die "$ARGV[0] is not a valid device. Exiting.\n";
}
my $DRIVE = $ARGV[0];
open(INFILE, "/proc/partitions") or die "Can't open file for read: $!";
print "\n";
while (<INFILE>){
$DRIVE =~ s/\/\w+\///g;
if ($_ =~ /($DRIVE$)/){
my ($D1, $D2, $D3, $D4, $D5) = split /\s+/;
$drive_size = sprintf "%0d", ($D4 / 1000);
}elsif ($_ =~ /($DRIVE[0-9])/){
my ($var1, $var2, $var3, $var4, $var5) = split /\s+/;
if ($var4 == 1){
print "Partition /dev/$var5 -- Extended --\n";
}
else {
$var4 = sprintf "%0d", ($var4 / 1000);
$totalused = ($totalused += $var4);
print "Partition /dev/$var5 used $var4 MB\n";
}
}
}
$free = ($drive_size - $totalused);
print "\nTotal drive size $drive_size MB\n";
print "Partitioned size $totalused MB\n";
print "Unpartitioned size $free MB\n";
print "\n";
close INFILE;