On 2005-11-20 16:48:12 -0800 (Sun, Nov), Jonathan Nichols wrote: > That being said, everything in the script works except for the few lines > that I've commented out, and the second to last line, where I try to > remove the files that are in the shared-maildir spam drop box. I've > tried quotes, full path names, backticks, everything that I can think > of, but the results end up being the same. > > Help? > > The script: > > [EMAIL PROTECTED] ~/bin $ cat eat_spam.pl > #!/usr/bin/perl > # > # Just a script to gather up spam, tar it up, > # and move it over to mailgate for further > # processing. > # > # Friday, May 13th, 2005.
Surely the main problem lies here - in the date. :-)
> #
> use warnings;
>
> # define some variables
> $spambucket = "/home/vmail/shared-maildirs/Spamdrop/.Incoming/cur";
> $canofspam = "spam.tar";
> # die if $canofspam is already there
> # die "$canofspam already exists, exiting...\n" if (-f "$canofspam");
> #if (-e "$canofspam") {
IIANM it should work... why it does not? Any error messages ?
> #system("tar -cf $canofspam $spambucket");
> #} else {
> print"Creating the tarball...\n";
> system("tar -rf $canofspam $spambucket");
I would rather use: tar -rf $canofspam -C $spambucket .
( -C = change to the directory first, [dot] = tar the current directory )
but that's minor thing.
Also it may be good to use absolute path for $canofspam, just for
elegance. You may try to achieve the great wisdom of 'mktemp' command.
> print"Created the tarball...\n";
> system("chown jnichols:users $canofspam");
> print"Changed permissions on $canofspam...\n";
> system("scp $canofspam [EMAIL PROTECTED]:~/");
> print"Moved $canofspam to mailgate...\n";
> system("rm $canofspam");
> system("ls $spambucket | while read f; do sudo rm \$f; done");
The problem here is that ls prints out bare filenames without path, then
rm is trying to remove files with given names, but is doing this in
CURRENT directory.
Quick fix may look like this: ls $spambucket/* | while read.....
or: ls $spambucket | while read f; do sudo rm $spambucked\$f; done
or something with cd $spambucket... or whatever you like.
>
> The results, and some stuff I've tried:
>
[...]
> [EMAIL PROTECTED] ~/bin $ for i in 'ls
> /home/vmail/shared-maildirs/Spamdrop/.Incoming/cur';do 'sudo rm $i'; done
> -bash: sudo rm $i: command not found
> [EMAIL PROTECTED] ~/bin $ for i in 'ls
> /home/vmail/shared-maildirs/Spamdrop/.Incoming/cur';do sudo rm $i; done
> rm: cannot remove `ls': No such file or directory
> rm: cannot remove `/home/vmail/shared-maildirs/Spamdrop/.Incoming/cur':
> Is a directory
Wrong quotes: ' and " are usually for hiding spaces and other special stuff.
You wanted `, but you may use $(command to execute) instead of `command
to execute` - if you are using bash. $() looks better IMHO.
> [EMAIL PROTECTED] ~/bin $ for i in `ls
> /home/vmail/shared-maildirs/Spamdrop/.Incoming/cur` ;do sudo cat $i; done
> cat:
> 1132422567.M220626P10407V0000000000000803I0004F9F5_5.mail,S=2307:2,S: No
> such file or directory
See how this is executed:
ls checks the files in directory /home/vma...../cur and prints a name of
found file: 11132...23123123
The name is assigned to the variable 'i', sudo receives two words:
'cat' and '1234242....' as a command to execute, executes
'cat', 'cat' looks for file 11132....23213131 and since the file is not
absolute path, cat looks for it in current directory.
Your script would work if you were in /home/vmail/.... before executing
it.
I suppose you may find it useful to read man bash, sections: "QUOTING" and
"Command Substitution".
And one small note: if you print message like "Removed some file", then
you should check whether previous command really did what it should,
else your program may be found lying ;-)
In your case it would be something like:
system("command to execute");
if( $? == 0 ) {
print "Command executed\n";
} else {
print "Failed miserably\n";
}
read perldoc -f system
...or use something like:
print "Executing command...";
system("command to execute");
HTH ;-)
--
No virus found in this outgoing message.
Checked by 'grep -i virus $MESSAGE'
Trust me.
pgpnGKnRy311D.pgp
Description: PGP signature
