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

I want to write a small Perl script that will output to a browser the contents of a directory in the format of the command line function ls -lt. I only want to see those files in the directory that end with the following: *.log, *.txt, *.sql, *.err, *.par, *.csv. I want to use this file to troubleshoot report problems on our production web server. (Developers do not have access to production at my company.)

Here is what I have written so far:

#!/usr/local/bin/perl5.00506 use CGI; $query = new CGI; $query_string = $query->param('DIR'); print "Content-type: text/plain", "\n\ print `ls -lt $query_string`;
I want do something like:
print `ls -lt *.sql *.txt *.log .. $query_string`;
However, this does not work. Any help that you can provide will be highly appreciated. Regards,

Dave

Replies are listed 'Best First'.
Re: Outputting contents of ls -lt
by marto (Cardinal) on May 04, 2005 at 16:29 UTC
    Hi Dave,

    #!/usr/bin/perl use strict; use CGI; my $query = new CGI; my $filter = "*.pl *.txt"; print "Content-type: text/plain"; my $lsoutput = `ls -lt $filter`; print "\n$lsoutput";

    I have included use strict; and a variable called $filter which, in the example provided, returns the output for the following command ls -lt *.pl *.txt

    Hope this helps

    marto
Re: Outputting contents of ls -lt
by marto (Cardinal) on May 04, 2005 at 17:39 UTC
    Hi again !

    About the use strict; thing.
    This is a good node (thanks runrig) to have a read at to introduce why to include use strict;, just incase you did not know already.

    Cheers,

    marto
Re: Outputting contents of ls -lt
by tlm (Prior) on May 04, 2005 at 19:08 UTC

    Why exactly did print `ls -lt ...` fail? Also, what information do you need from the output of ls -lt? Some it can be obtained quite easily by other means (e.g. -s, -M) than don't require invoking ls. Here's a quick, poor-man's ls:

    use strict; use warnings; my $t = time; my $spd = 24*60*60; printf "%d\t%s\t%s\n", -s, scalar(localtime($t+$spd*-M)), $_ for <*.{log,txt,sql}>;

    BTW, it is probably not a good idea to stick an input string right into backticks like you have in your sample code; what if someone gave you an input string like '; rm -rf * *.*' ?

    the lowliest monk

Re: Outputting contents of ls -lt
by TedPride (Priest) on May 04, 2005 at 16:26 UTC
    Put the return value from ls into a variable, and filter the file names for the extensions you want to allow. It's better to do it this way than filtering through ls, imho.

    EDIT: Here's some code:

    #!/usr/local/bin/perl use strict; use warnings; my $base = '/u/web/user/'; my @allow = qw/log txt sql err par csv/; my $e; print "Content-type: text/plain\n\n"; for (split /\n/, `ls -lt $base`) { for $e (@allow) { if (m/\.$e$/) { print "$_\n"; last; } } }
    The advantage of doing it this way is that your code is easy to modify to work with rejected lines, should you decide you want to in the future.