Errors in Perl when using awk command

Hi Guys,

Hope everyone is fine :slight_smile:
I have this code below:

#!/usr/bin/perl

$num_of_files=`ls | grep -v remover | wc -l`;
$remover=`ls -lrt | grep -v total | grep -v remover | head -1 | awk '{print $8}' | rm \`xargs\``;

if ($num_of_files>3) {
        system ($remover);
}

When I tried to execute it, found an error which looks like this below:

rm: invalid option -- 'w'
Try `rm --help' for more information.

What is the meaning of this error and how can I make my script work? Please advise.

br,
rymnd_12345

Given what you've shown, I'd say you just need to wrap your vars with single quotes when they're used; ie, '$var'. This may not be it, as it's balking at barewords, but it allows for the proper shell-out that you'd defined...but then again given what we've seen, if that's the extent of your script, why not just leave it as a shell script? Perl isn't even needed under the circumstances.

Hi curleb,

Thanks for the info, for now I've already created one using shell script this time. The script is now working and later I will put it in CRON.

Our server can only accept CRON entries provided you're a root user, so I've tried to input the script as a root user, my problem is that it's not functioning. Here is the analogy below:

Files that I want to remove are located in /home/user/USER_LOGS/, the script is also located in this directory. This is the script:

#!/usr/bin/sh

num_of_files=`ls /home/nsadm/USER_LOGS/ | grep UserActivity | grep UMB00 | wc -l`

if [ $num_of_files -gt 3 ]
then
remove=`ls -lrt /home/nsadm/USER_LOGS/ | grep UserActivity | grep UMB00 | head -1 | awk '{print $8}' | rm \`xargs\``
$remove
fi

but as per running the script when I'm in the directory /root (this is the default directory upon login as root user), it can't see the file and therefor it can't perform the rm function. This is what I've did as root user:

root:~ # /home/user/USER_LOGS/remover.sh
rm: cannot remove `09:46': No such file or directory

I think the problem here is that it can't locate the files that I want to delete under the /root directory. Can you help me how to enhance my script so that if ever I would like to put it in CRON it will function normally, by removing file on top and retaining only 3 files on it.

Please advice,
rymnd_12345

your file list includes some info from the long listing (ls -lrt) by accident ... confirm by running the code below by itself ...

ls -lrt /home/nsadm/USER_LOGS/ | grep UserActivity | grep UMB00 | head -1 | awk '{print $8}'

... use "ls -rt" instead of "ls -lrt" ...