perl program with "&"

Discussion in 'Perl Misc' started by yezi, Aug 3, 2005.

  1. yezi

    yezi Guest

    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
    yezi, Aug 3, 2005
    #1
    1. Advertising

  2. yezi

    Ian Wilson Guest

    yezi wrote:
    > 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?
    Ian Wilson, Aug 4, 2005
    #2
    1. Advertising

  3. yezi

    Guest

    yezi <> wrote:
    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 <> 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
    , Aug 4, 2005
    #3
  4. yezi wrote:

    > 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
    ????? ????? (Shantanoo Mahajan), Aug 11, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,767
  2. Christopher M. Lusardi
    Replies:
    3
    Views:
    180
    Alan J. Flavell
    Oct 19, 2003
  3. PerlFAQ Server

    FAQ 1.13 Is it a Perl program or a Perl script?

    PerlFAQ Server, Jan 20, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    118
    PerlFAQ Server
    Jan 20, 2011
  4. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    288
    PerlFAQ Server
    Jan 23, 2011
  5. PerlFAQ Server

    FAQ 1.13 Is it a Perl program or a Perl script?

    PerlFAQ Server, Apr 11, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    107
    PerlFAQ Server
    Apr 11, 2011
Loading...

Share This Page