Click to See Complete Forum and Search --> : Why doesn't this work?


Energon
05-07-2001, 01:51 PM
I have this in a Perl script:


system("$find $sysdir/*.* -mtime +7 -exec rm -rf {} \\;");


$find = /usr/bin/find
$sysdir = /var/log

if I run the script, it never deletes the old files... I'm not sure what's going on... I don't get any kind of an error from it and I know that it's getting to this spot in the code... is there a more Perl way of doing it instead of relying on the shell doing it?

YaRness
05-07-2001, 01:58 PM
i dunno enough about the find command, but i guess i can ask this:


{} \\

what's the {} for, and did you mean a / where you have a \\ (which ends up being a \ when it gets to the shell i think)? also, you may want to put in some escaped double-quotes (i.e. \") around the rm command maybe. i'm just kinda guessing here.

perl has an unlink command that you could pass a list of files to if that's what you are extracting with the rest of that code. i think


@list = `<my system command that returns a list of files>`;
unlink @list;

or maybe "unlink foreach (@list)" though they are prolly both the same (note: i verified this, you can just do unlink @list). also you'd hafta make sure that each item in lists has a full path.. or at least a path relevant to where the script is executed from.

"perldoc -f unlink"

Energon
05-07-2001, 02:01 PM
how can I generate the @list? It's basically any file (not directory) older than 7 days... ie, logs get rotated every night and I wanna get rid of those ones to save space... know what I mean?

The stuff in the find command is right, if I use just bash (and make the \\ just a \) it works just fine... it's only since I switched my scripts to perl that this has stopped working... but if I can get a more "perl" way of doing it, I'd like to... I just don't know how I'd generate my list of files to unlink...

Energon
05-07-2001, 02:08 PM
see, I have this now:


#! /usr/bin/perl -w
use strict;

my @files = `/usr/bin/find /var/log/*.* -mtime +7`;
foreach my $f (@files) {
print $f."\n";
}
unlink @files;


it prints this:

/var/log/backup.log.04-28-2001-2300

/var/log/cron.04-28-2001-2300

/var/log/debug.04-28-2001-2300

/var/log/messages.04-28-2001-2300

/var/log/sulog.04-28-2001-2300

/var/log/syslog.04-28-2001-2300

/var/log/tcp.allow.04-28-2001-2300

/var/log/tcp.deny.04-28-2001-2300

but it never deletes them...

YaRness
05-07-2001, 03:02 PM
try

foreach (@files)
{
chomp;
unlink($_) or die "unlink fudged up: $?, $!\n";
}

or maybe even "tr/\n//d" instead of chomp.

Energon
05-07-2001, 04:46 PM
man... that was it... I needed to chomp the filename... stupid mistake on my part... thanks for reminding me... :)