St. Catharine has asked for the wisdom of the Perl Monks concerning the following question:

given the name of a mount point which is contained in $mp, what is the perl equivalent of df -kl.

I need to know the total capacity of several mount points whose names follow standard formats.

I tried system(df -k $mp), but get back the two line junk. I want a command which give the $mp, returns come way of eventually getting the mount point capacity...

much thanks,
perlifying my korn shell,
St. C.

Edited by Chady -- fixed df -kl typo, and added formatting while I'm at it.

Replies are listed 'Best First'.
Re: Calculating Mount Point Capacity
by kvale (Monsignor) on Apr 20, 2004 at 02:55 UTC
    Backticks `` are used to get the results of a shell command:
    1042% df -k /dev/hda7 Filesystem 1K-blocks Used Available Use% Mounted on /dev/hda7 9606556 7156344 1962216 79% / 1043% perl -e'print `df -k /dev/hda7`' Filesystem 1K-blocks Used Available Use% Mounted on /dev/hda7 9606556 7156344 1962216 79% /
    Update I forgot to answer the pure perl part of the question. The module Filesys::Df should be able to do what you want. From the synopsis:
    use Filesys::Df; my $ref = df("/tmp"); # Default block size of 1024 bytes. # You can specify a different block # as a second argument. print"Percent Full: $ref->{per}\n"; print"Total Blocks: $ref->{blocks}\n"; print"Blocks Available: $ref->{bavail}\n"; print"Blocks Used: $ref->{used}\n"; print"Inode Percent full: $ref->{fper}\n"; print"Inodes Free: $ref->{favail}\n"; print"Inodes Used: $ref->{fused}\n"; print"Total Bytes", ($ref->{blocks} * 1024), "\n";

    -Mark

Re: Calculating Mount Point Capacity
by emilford (Friar) on Apr 20, 2004 at 02:57 UTC
    Couldn't you just use

    my @output = `df -k $mp`;

    and then just use a regex to grab what you need?

    Edit: Damn, somebody beat me to it. What he said.
Re: Calculating Mount Point Capacity
by Errto (Vicar) on Apr 20, 2004 at 03:04 UTC

    I'm not sure how to get that information in a pure perl way. But the following works great on my Linux system:

    $_ = (`df -k /home`)[1]; my $blocks = (split)[1]; print "$blocks\n";

    The first line of df output is column headers, so it skips that and takes the second line. Then it splits that on whitespace and takes the second token, the block count.

Re: Calculating Mount Point Capacity
by TilRMan (Friar) on Apr 20, 2004 at 03:50 UTC

    Presumably some chap has written a module that'll do what you need, but I don't know of one. If you can do system("df"), though, why not? (After all, Perl is good at parsing this kind of stuff.)

    By "the two line junk," do you mean the header line or the line wrapping/splitting when the device name is too long?

    My df (fileutils 4.1) takes a -P (a.k.a. POSIXLY_CORRECT) which forces each record to be on the same line. Uglier for humans; prettier for Perl. But in case yours doesn't:

    # capacity($mountpoint) # # Returns the capacity (in 1024-byte units) of the # filesystem at $mountpoint. # sub capacity { my ($mp) = @_; my ($head, $line, $extra) = `df -kl $mp`; $line .= $extra if defined $extra; return (split ' ', $line)[1]; }

    Before using this function, be sure to consider: security ($mp is passed to the shell), error handling, efficiency (you will have to spawn a subshell for every filesystem), and portability (not every OS has df).