Gentoo Archives: gentoo-server

From: "Mariusz Pękala" <skoot@××.pl>
To: gentoo-server@l.g.o
Subject: Re: [gentoo-server] Help me with a perl script..
Date: Mon, 21 Nov 2005 08:20:49
Message-Id: 20051121081843.GA10193@lisa.tutaj
In Reply to: [gentoo-server] Help me with a perl script.. by Jonathan Nichols
1 On 2005-11-20 16:48:12 -0800 (Sun, Nov), Jonathan Nichols wrote:
2 > That being said, everything in the script works except for the few lines
3 > that I've commented out, and the second to last line, where I try to
4 > remove the files that are in the shared-maildir spam drop box. I've
5 > tried quotes, full path names, backticks, everything that I can think
6 > of, but the results end up being the same.
7 >
8 > Help?
9 >
10 > The script:
11 >
12 > jnichols@mail ~/bin $ cat eat_spam.pl
13 > #!/usr/bin/perl
14 > #
15 > # Just a script to gather up spam, tar it up,
16 > # and move it over to mailgate for further
17 > # processing.
18 > #
19 > # Friday, May 13th, 2005.
20
21 Surely the main problem lies here - in the date. :-)
22
23 > #
24 > use warnings;
25 >
26 > # define some variables
27 > $spambucket = "/home/vmail/shared-maildirs/Spamdrop/.Incoming/cur";
28 > $canofspam = "spam.tar";
29 > # die if $canofspam is already there
30 > # die "$canofspam already exists, exiting...\n" if (-f "$canofspam");
31 > #if (-e "$canofspam") {
32
33 IIANM it should work... why it does not? Any error messages ?
34
35 > #system("tar -cf $canofspam $spambucket");
36 > #} else {
37 > print"Creating the tarball...\n";
38 > system("tar -rf $canofspam $spambucket");
39
40 I would rather use: tar -rf $canofspam -C $spambucket .
41 ( -C = change to the directory first, [dot] = tar the current directory )
42 but that's minor thing.
43
44 Also it may be good to use absolute path for $canofspam, just for
45 elegance. You may try to achieve the great wisdom of 'mktemp' command.
46
47 > print"Created the tarball...\n";
48 > system("chown jnichols:users $canofspam");
49 > print"Changed permissions on $canofspam...\n";
50 > system("scp $canofspam jnichols\@192.168.10.3:~/");
51 > print"Moved $canofspam to mailgate...\n";
52 > system("rm $canofspam");
53 > system("ls $spambucket | while read f; do sudo rm \$f; done");
54
55 The problem here is that ls prints out bare filenames without path, then
56 rm is trying to remove files with given names, but is doing this in
57 CURRENT directory.
58 Quick fix may look like this: ls $spambucket/* | while read.....
59 or: ls $spambucket | while read f; do sudo rm $spambucked\$f; done
60 or something with cd $spambucket... or whatever you like.
61
62 >
63 > The results, and some stuff I've tried:
64 >
65 [...]
66 > jnichols@mail ~/bin $ for i in 'ls
67 > /home/vmail/shared-maildirs/Spamdrop/.Incoming/cur';do 'sudo rm $i'; done
68 > -bash: sudo rm $i: command not found
69 > jnichols@mail ~/bin $ for i in 'ls
70 > /home/vmail/shared-maildirs/Spamdrop/.Incoming/cur';do sudo rm $i; done
71 > rm: cannot remove `ls': No such file or directory
72 > rm: cannot remove `/home/vmail/shared-maildirs/Spamdrop/.Incoming/cur':
73 > Is a directory
74
75 Wrong quotes: ' and " are usually for hiding spaces and other special stuff.
76 You wanted `, but you may use $(command to execute) instead of `command
77 to execute` - if you are using bash. $() looks better IMHO.
78
79 > jnichols@mail ~/bin $ for i in `ls
80 > /home/vmail/shared-maildirs/Spamdrop/.Incoming/cur` ;do sudo cat $i; done
81 > cat:
82 > 1132422567.M220626P10407V0000000000000803I0004F9F5_5.mail,S=2307:2,S: No
83 > such file or directory
84
85 See how this is executed:
86 ls checks the files in directory /home/vma...../cur and prints a name of
87 found file: 11132...23123123
88
89 The name is assigned to the variable 'i', sudo receives two words:
90 'cat' and '1234242....' as a command to execute, executes
91 'cat', 'cat' looks for file 11132....23213131 and since the file is not
92 absolute path, cat looks for it in current directory.
93 Your script would work if you were in /home/vmail/.... before executing
94 it.
95
96 I suppose you may find it useful to read man bash, sections: "QUOTING" and
97 "Command Substitution".
98
99 And one small note: if you print message like "Removed some file", then
100 you should check whether previous command really did what it should,
101 else your program may be found lying ;-)
102
103 In your case it would be something like:
104 system("command to execute");
105 if( $? == 0 ) {
106 print "Command executed\n";
107 } else {
108 print "Failed miserably\n";
109 }
110
111 read perldoc -f system
112
113 ...or use something like:
114 print "Executing command...";
115 system("command to execute");
116
117 HTH ;-)
118
119 --
120 No virus found in this outgoing message.
121 Checked by 'grep -i virus $MESSAGE'
122 Trust me.