Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 556
  • Last Modified:

Need help to complete my perl script


I'm trying to develop a perl script (for Linux platform) that will take 'fdisk -l' command output as input and print the Boot device and its disk size.

Below shown are the portion of 'fdisk -l' output and the script which I developed. I need your help to complete this script. I wish to print the 'Size of Boot disk" as well.  Please help me in this.

Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device      Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1           6       48163+  83  Linux


open FILE, "fdisk -l |" or die $!;
   if ( $_ =~ m/(\/dev\/\S+)\s+(\*)\s+/ )
        print "Boot partition : $1\n";
        $boot_disk = "$1";
        @value1 = split("1",$boot_disk);
        $boot_device = $value1[0];
        print "Boot device : $boot_device \n";


Expected Result:
Boot partition : /dev/sda1
Boot device : /dev/sda
Size of Boot disk: 21.4 GB       <-- My script is missing this.

Some exceptions that we need to take care:
i) The Boot partition needn't be first partition. It can be sda2, sda3 and so on.  Hence you may have to change my 'split' statement.
ii) The boot device path needn't be /dev/sdaX.  It can be /dev/mapper/boot1 or something else.  I'm actually taking '*' as the catching item to determine the disk is a Boot device.
1 Solution
open FILE, "fdisk -l |" or die $!;                                                                                      
while( <FILE> ){
    ($dev,$size)=($1,$2) if /Disk\s+(\S+):([^,]*)/;
    print "Boot partition : $1\nBoot device : $dev\nSize of Boot disk:$size\n" if m((/dev/\S+)\s+\*);
ashsysadAuthor Commented:
Hello Ozo,  As usual you gave me your magical syntax with just 2 lines. I have learnt a lot in the past couple of years.  Thanks a lot again !!!

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now