Alexander Bokedal
Alexander Bokedal

Reputation: 29

How do I extract device info and mountpoints from fstab using Perl?

I'm new to Perl and I really need help witch a specific issue.

I need to extract info from my fstab, but there's a lot of information in there and I only want the information about the devices and their mount points. The closest I got to finding an answer was: http://www.freebsd.org/doc/en/articles/vinum/perl.html

But since I'm new to Perl I have a hard time tweaking the code so it helps me with my problem

This is my fstab, but I only want the 3 "dev" lines including mountpoints, any smart way to do this?

/dev/disk/by-id/usb-ST925041_0AS_FF9250410A0000000000005FF87FF7-part2 /                    
ext3       noatime,nodiratime,acl,user_xattr 1 1
/dev/disk/by-id/usb-ST925041_0AS_FF9250410A0000000000005FF87FF7-part3 /var/log             
ext3       noatime,nodiratime,acl,user_xattr 1 2
/dev/disk/by-id/usb-ST925041_0AS_FF9250410A0000000000005FF87FF7-part1 swap                 swap       
defaults              0 0
proc                 /proc                proc       defaults              0 0
sysfs                /sys                 sysfs      noauto                0 0
debugfs              /sys/kernel/debug    debugfs    noauto                0 0
usbfs                /proc/bus/usb        usbfs      noauto                0 0
devpts               /dev/pts             devpts     mode=0620,gid=5       0 0

Help is very appreciated, thanks in advance!

Upvotes: 1

Views: 1098

Answers (2)

Daniel Kamil Kozar
Daniel Kamil Kozar

Reputation: 19266

Something like this should be just fine, then :

#!/usr/bin/perl

open (my $fstab, "<", "/etc/fstab") or die "Cannot open /etc/fstab.";

while(<$fstab>)
{
    my @list = split;
    next if($list[0] !~ m,^/dev,);
    print "Device : $list[0]\nMountpoint : $list[1]\n";
}

close($fstab);
exit 0;

Keep in mind that this will not work if your fstab has UUID= entries or any kind of file systems that aren't devices listed in /dev.

Upvotes: 1

TLP
TLP

Reputation: 67900

If that is your output, and you just want to grab the lines that start with /dev, you can simply pipe it to grep, without altering your perl script.

perlscript.pl | grep "^/dev"

Not sure if that works without the -e flag, its been a while and I can't test it right now. If all else fails, use perl:

perlscript.pl | perl -nwe 'print if m#^/dev#'

Upvotes: 2

Related Questions