in reply to Re: Re: Formatted Text
in thread Formatted Text

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`.

Replies are listed 'Best First'.
Re: Re: Re: Re: Formatted Text
by TekWannaBe (Initiate) on Jan 30, 2004 at 17:05 UTC
    This is the current code and the error returned when ran.
    format DISKSPACE = ($Filesystem,$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 spacea +vail.txt"; open (DISKSPACE, "> spaceavail.txt") || die "Error opening file spacea +vail.txt"; while (<DISKSPACE>) { chop; ($Filesystem, $kbytes, $used, $avail, $capacity, $Mounted_on) = spli +t (/ /); 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"; } $ ./testmenu.pl My First Perl Menu A. Current Date & Time B. Users currently logged in C. Name of working directory D. Contents of the working directory E. Names & Login names of user currently logged on to aries F. Aries Disk Space Utilization Enter A, B, C, D, E, F: F Filesystem kbytes used avail capacity Mounted on /dev/dsk/c1t0d0s0 2016343 1392038 563815 72% / /proc 0 0 0 0% /proc mnttab 0 0 0 0% /etc/mnttab fd 0 0 0 0% /dev/fd swap 3888816 40 3888776 1% /var/run swap 3888848 72 3888776 1% /tmp /dev/dsk/c1t0d0s7 32442879 523718 31594733 2% /export/home Filehandle DISKSPACE opened only for output at ./testmenu.pl line 46.
      You are trying to use the same filehandle DISKSPACE for reading the output of df and for writing your formatted result. Get rid of the system "df -k \n"; in the main code, change your first open in df_total to:
      open DISKSPACEIN, "df -k|" or die "couldn't start df -k: $!";
      (The | means to execute the given command and make it's output available via the filehandle. See perlipc.)

      Then change your while (<DISKSPACE>) to while (<DISKSPACEIN>).

      It's also an especially good idea when using pipes (the "df -k|" argument to open makes what's called a pipe) to explicitly close the filehandle and check the result, so after the while loop, put:

      close DISKSPACEIN or die "got df error: $! $?";
      See perlipc for more about this.