Need help to complete my perl script

Posted on 2012-08-18
Last Modified: 2012-08-18

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.
Question by:ashsysad
    LVL 84

    Accepted 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+\*);

    Author Closing Comment

    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

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
    Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now