Fedora Linux Support Community & Resources Center
  #1  
Old 29th August 2006, 08:20 PM
homey Offline
Registered User
 
Join Date: Feb 2005
Posts: 415
Convert bash script to perl script

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;

Last edited by homey; 2nd September 2006 at 05:19 AM.
Reply With Quote
  #2  
Old 2nd September 2006, 04:24 AM
homey Offline
Registered User
 
Join Date: Feb 2005
Posts: 415
Here's another take on the perl script using format
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];
print "\n";

open(INFILE, "/proc/partitions") || die "Can't open file for read: $!";
while (<INFILE>){

$DRIVE =~ s/\/\w+\///g;

if ($_ =~ /($DRIVE$)/){
   ($D1, $D2, $D3, $D4, $D5) = split /\s+/;

} elsif ($_ =~ /($DRIVE[0-9])/){
   ($var1, $var2, $var3, $var4, $var5) = split /\s+/;
      if ($var4 == 1){
         print "Partition /dev/$var5   --- Extended ---\n";
      }
      else {
         $totalused = ($totalused += $var4);
         write (STDOUT);
      }
   }
}
$~ = "ENDING";
write (STDOUT);

format STDOUT =
Partition /dev/@<<<< used @######## MB
$var5, ($var4/1000),  $var1,$var2,$var3
.

format ENDING =

Total drive size   @######## MB
($D4/1000),  $D1,$D2,$D3,$D5
Partitioned size   @######## MB
($totalused/1000)
Unpartitioned size @######## MB
(($D4-$totalused)/1000)

.
#### Just so you don't bork that last period ( . )
Reply With Quote
Reply

Tags
bash, convert, perl, script

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash or perl: rename script fedora08 Programming & Packaging 4 24th February 2008 02:12 PM
Convert a KSH script to a RPM package TheH1982 Using Fedora 2 17th October 2007 07:06 PM
awk/sed script to convert formatting? johannlo Using Fedora 6 1st January 2007 10:50 PM
script to convert wmv to mpg2 jim Programming & Packaging 2 26th July 2005 11:01 AM
Perl Script Help codontek Programming & Packaging 3 27th August 2004 12:09 PM


Current GMT-time: 12:42 (Thursday, 20-06-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat