perl program with "&"

Y

yezi

Dear ALl:

The program now is following:
#!/usr/bin/perl
use warnings;
use strict;

my $filename;
my $cmdline;

$filename="/home/binye/test_perl/testdownload.txt";
open( FILE, "< $filename" ) or die "Can't open $filename : $!";

while( <FILE> ) {

print "Processing wget.";
my $cmdline="wget -w 50 -q http://$_ & tcpdump -c 20 -i eth0 src host
$_ -w $_.dmp ";
system($cmdline);
print "done ...w $_..";

}

close FILE;

I try to run it, by using the & the waring message is :

syntax error near unexpected token `&'
sh: -c: line 2: ` & tcpdump -c 20 -i eth0 src host www.cron.com'

Then I exchange the & to \$, still same problem.

So I donot know how to make the program works now. Thanks for any
comments
 
I

Ian Wilson

yezi said:
Dear ALl:

The program now is following:

Is this a followup to an earlier posting? Please read the posting
guidelines at
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
#!/usr/bin/perl
use warnings;
use strict;

my $filename;
my $cmdline;

$filename="/home/binye/test_perl/testdownload.txt";
open( FILE, "< $filename" ) or die "Can't open $filename : $!";

while( <FILE> ) {

print "Processing wget.";
my $cmdline="wget -w 50 -q http://$_ & tcpdump -c 20 -i eth0 src host
$_ -w $_.dmp ";

You may want to check that $cmdline actually contains what you think it
does.
system($cmdline);
print "done ...w $_..";

}

close FILE;

I try to run it, by using the & the waring message is :

syntax error near unexpected token `&'
sh: -c: line 2: ` & tcpdump -c 20 -i eth0 src host www.cron.com'

The "sh" at the beginning means that this is not a perl error message,
it is a shell error message. your system call passes your command to the
shell. This means that this isn't really a perl question.

Then I exchange the & to \$, still same problem.

You need to understand shell programming. I suspect that randomly
changing symbols rarely achieves anything useful.
So I donot know how to make the program works now. Thanks for any
comments

I'd try typing your command into a shell command line before embedding
it in a perl script.

I suspect you need to start tcpdump before running wget (assuming you
want to capture some HTTP traffic). If that is the case then I think
'tcpdump <options> ; wget <options>' would be more appropriate.

I don't know why you'd want to do this since wget has some handy options
for capturing the HTML and host responses etc.

In fact I don't know why you are using system('wget') instead of a perl
HTTP module - wouldn't that make it a lot easier?
 
C

chris-usenet

Y> while( <FILE> ) {
Y> my $cmdline="wget -w 50 -q http://$_ & tcpdump -c 20 -i eth0
src host $_ -w $_.dmp ";
Y> system($cmdline);


Ian Wilson <[email protected]> suggested:
IW> You may want to check that $cmdline actually contains what you
IW> think it does.

This is an excellent suggestion.


Y> I try to run it, by using the & the waring message is :
Y> syntax error near unexpected token `&'
Y> sh: -c: line 2: ` & tcpdump -c 20 -i eth0 src host www.cron.com'

IW> The "sh" at the beginning means that this is not a perl error message,
IW> it is a shell error message. your system call passes your command
IW> to the shell. This means that this isn't really a perl question.

Actually, in this case it is a perl related problem. The hint is that
sh is complaining about line 2. I'll wager that the OP didn't expect
there to be more than one line in their system() shell call.


IW> I'd try typing your command into a shell command line before embedding
IW> it in a perl script.

The command will work, because it won't be exactly the same as what's
passed via system(). It's Yezi's misunderstanding of what it returned
from <FILE> that's at fault here.

Yezi, I suggest you go and read "perldoc -f chomp" and see if that helps
resolve the problem you're seeing.

Chris
 
?

????? ????? (Shantanoo Mahajan)

yezi said:
Dear ALl:

The program now is following:
#!/usr/bin/perl
use warnings;
use strict;

my $filename;
my $cmdline;

$filename="/home/binye/test_perl/testdownload.txt";
open( FILE, "< $filename" ) or die "Can't open $filename : $!";

while( <FILE> ) {

print "Processing wget.";
my $cmdline="wget -w 50 -q http://$_ & tcpdump -c 20 -i eth0 src host
$_ -w $_.dmp ";
system($cmdline);
print "done ...w $_..";

}

close FILE;

Are you trying to run wget in background? If yes, have a look at '-b' or
'--background' option of wget.

Regards,
Shantanoo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top