in reply to how to pass colon and comma in exec args

Use the exec PROGRAM LIST form to call the external program. You will probably need a full path to the executable, but the shell won't mangle your arguments.

# actually a list of tokens, not one string. exec qw{/usr/bin/tcpreplay -i eth2 -N 192.168.0.1/32:1.1.1.1/32,192.16 +8.0.2/32:2.2.2.2/32 test.cap};

Replies are listed 'Best First'.
Re^2: how to pass colon and comma in exec args
by billbabcock (Acolyte) on Nov 05, 2004 at 05:53 UTC
    So, of course, I simplified things a bit in the post. I'm using variables for this:

    exec qw{$tcpreplay -p $pps -i $eth -N $src_ip/32:$src,$dest_ip/32:$users->{$user}->{nets}{$src} $users->{$user}->{authcap} $datacap 2>&1}

    would be the equiv to your response in my code.

    That doesn't work, tho'. I get an error about 'No such file or directory' - but the binary is in the location specified in the variable.

    I've also gone the route of creating an @arg array with each token in it, and call it so:

    exec(@args) or die .....

    and I get the result that I posted.

    Thanks!
      I also tried the qw with a different delimiter to avoid confusion and collision with my variables. :-)
        'man perlop' says qw{} doesn't interpolate variables.
        If you want to avoid the shell messing up your arguments, you could try the 'indirect object'-form of exec.
        my $cmd = '/usr/bin/tcpreplay'; # or whereever exec {$cmd} ($cmd, '-p', $pps, '-i', $eth); # and so on...
        Note there is no comma after the first arg to exec. With this form exec will not use the shell and pass the list of args unchanged as argv[] to the command. This means you have to supply a value for argv[0], too (something one might forget coding too much perl...) Hope this helps....