Re: [SLUG] simple perl script problem

From: Scotty Logan (scotty@scottylogan.com)
Date: Wed Mar 09 2005 - 17:22:09 EST


aaron steimle wrote:
> It will not print any part of the @fileinfo array..but what is really
> happening is stat will not work on the $filename variable.
> Just change the DIR and run it yourself.

and the code

> #!/usr/bin/perl
>
> opendir DIR, "/var/errors/";
> while ( $filename = readdir(DIR) )
> {
> @fileinfo=stat($filename);
> $sizestat = $fileinfo[7]; print "$filename - $sizestat\n";
> }
> closedir DIR;

I hate to be a smartass, but the problem would be trivial to spot if you
checked for errors :-)
For example,

        @fileinfo = stat($filename)
                 or die "Can't stat $filename: $!";

would output an error message like:

        Can't stat messages: No such file or directory

As written, the script will only work if it's run in the '/var/errors/'
directory. readdir returns file names, not file paths, so the stat will
always fail (unless there happens to be a file in the current directory
with the same name as one in /var/errors).

Try this instead:

#!/usr/bin/perl

$dir = '/var/log';
opendir DIR, $dir
   or die "Can't open $dir: $!";

while ( $filename = readdir(DIR) ) {
   @fileinfo=stat($dir . '/' . $filename)
     or die "Can't stat $filename: $!";
   printf "%s - %d\n", $filename, $fileinfo[7];
}
closedir DIR;

   Scotty
-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS). Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 20:26:00 EDT