TekWannaBe has asked for the wisdom of the Perl Monks concerning the following question:

I am attempting add a formatted report from data computed from disk space. I am suppose to move the code for computing this space so a subroutine. It just ain't happening. Can someone please give me some insight. Here is the code: Nothing is formatted to my output file.
#!/usr/bin/perl -w format DISKSPACE = ($Filesytem,$kbytes, $used, $avail, $capacity, $Mounted_on) = split (/ + /); ====================== |@<<<<<<<<<<<<<<<<<<<| $FileSystem |@<<<<<<<<<<<<<<<<<<<| $kbytes |@<<<<<<<<<<<<<<<<<<<| $used |@<<<<<<<<<<<<<<<<<<<| $avail |@<<<<<<<<<<<<<<<<<<<| $capacity |@<<<<<<<<<<<<<<<<<<<| $Mounted_on ====================== . sub df_total { # if ($answer eq "F") { # write ($answer); open (DISKSPACE,">spaceavail.txt") || die "Error opening file spaceava +il.txt"; while (<DISKSPACE>) { chop; ($Filesytem,$kbytes, $used, $avail, $capacity, $Mounted_on) = split +(/ /); write (DISKSPACE); close (DISKSPACE) } } print "\n My First Perl Menu\n"; print "A. Current Date & Time\n"; print "B. Users currently logged in\n"; print "C. Name of working directory\n"; print "D. Contents of the working directory\n"; print "E. Names & Login names of user currently logged on to aries\n" +; print "F. Aries Disk Space Utilization\n"; print "Enter A, B, C, D, E, F: \n"; $answer = <STDIN>; chomp ($answer); if ($answer eq "A") { system "date \n"; } elsif ($answer eq "B") { system "who \n"; } elsif ($answer eq "C") { system "pwd \n"; } elsif ($answer eq "D") { system "ls -Fla \n"; } elsif ($answer eq "E") { system "finger \n"; } elsif ($answer eq "F") { system "df -k \n"; &df_total } else { print "The selection you entered: $answer, is incorrect. Please ent +er A, B, C or D \n"; }

Replies are listed 'Best First'.
Re: Formatted Text
by Roger (Parson) on Jan 30, 2004 at 05:00 UTC
    You have a few mistakes in your code -
    1) you openned DISKSPACE for writing yet you are reading from it.
    2) you had typo/spelling mistakes in the code.
    3) you should use qx or `` to capture output from df.

    I have written a demo on how to format df output according to your format.

    #!/usr/bin/perl -w format DISKSPACE = ====================== |@<<<<<<<<<<<<<<<<<<<| $FileSystem |@<<<<<<<<<<<<<<<<<<<| $kbytes |@<<<<<<<<<<<<<<<<<<<| $used |@<<<<<<<<<<<<<<<<<<<| $avail |@<<<<<<<<<<<<<<<<<<<| $capacity |@<<<<<<<<<<<<<<<<<<<| $Mounted_on ====================== . sub df_total { my @lines = split /\n+/, shift; open (DISKSPACE, ">spaceavail.txt") || die "Error opening file space +avail.txt"; for (@lines) { ($FileSystem, $kbytes, $used, $avail, $capacity, $Mounted_on) = sp +lit (/\s+/); write (DISKSPACE); } close (DISKSPACE) } my $result = `df -k`; df_total($result);

      Thanks Roger....I did attempt this but the code does the same thing, I do not get the output from df -k formatted in the file get no output to the file spaceavail.txt
        Interesting, are you sure? The code I posted has been tested and works for sure. Perhaps you can post your updated code here and let's see which part is not working.

Re: Formatted Text
by Anonymous Monk on Jan 30, 2004 at 08:35 UTC

    Do not reinvent the wheel.

    The modules Filesys::Df and Tie::DiskUsage (synonym for 'du') are attempts to approach such problems; both are available on CPAN.
Re: Formatted Text
by ysth (Canon) on Jan 30, 2004 at 09:03 UTC
    Seeing the code that was working as well as the code that doesn't work would be helpful. As is, it's kind of a guessing game. Do you want the output of "df -k" to go to the screen, or is that what you are trying to nicely format? Do you want the result of the format to go to the screen or to a file? Why do you want it in a subroutine? Is that just to make the main code cleaner or do you need to use it somewhere else as well? If the latter, what is needed in that somewhere else? Output to the screen? The formatted output returned in a variable?
      I would like the output of df -k to go to a file (spaceavail) nicely formatted. I need the subroutine, it is apart of my lab...gottah big learning curve. The code thus far puts nothing in the file, I did have it adding the formated box but no text from df -k output. Now that I wrote over what I did earlier I am not sure if I could get it to do the same again. Thanks for all your help.
        Then what you have should be ok; except that you want to read the output of df -k, not DISKSPACE. Do this like:
        open my $dfout, "df -k|" or die "couldn't start df -k: $!"; open DISKSPACE as you do while (<$dfout>) { ... } close $dfout or die "df failed: $! $?";
        or use the suggestion of capturing and splitting up the output with `df -k`.