FedoraForum.org - Fedora Support Forums and Community
Results 1 to 2 of 2
  1. #1
    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.

  2. #2
    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 ( . )

Similar Threads

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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •