views:

232

answers:

4

Hi guys, I am making a system call from perl for ContentCheck.pl and passing parameters with directories (having spaces). So I pass them in quotes, but they are not being picked up in the ContentCheck.pl file

Random.pm 1)

my $call = "$perlExe $contentcheck -t $target_path -b $base_path -o $output_path -s $size_threshold";
print "\ncall: ".$call."\n";
system($call);

Contentcheck.pl

    use vars qw($opt_t $opt_b $opt_o $opt_n $opt_s $opt_h);  # initialize
    getopts('t:b:o:n:s:h') or do{ 
    print "*** Error:  Invalid command line option.  Use option -h  for help.\a\n";
    exit 1};

    if ($opt_h) {print $UsagePage; exit; }

    my $tar;
    if ($opt_t) {$tar=$opt_t; print "\ntarget ".$tar."\n";} else {
    print " in target";
    print 
     "*** Error:  Invalid command line option.  Use option -h  for help.\a\n";
    exit 1;}
    my $base;
    if ($opt_b) {$base=$opt_b;} else {
    print "\nin base\n";
    print "*** Error:  Invalid command line option.  Use option -h  for help.\a\n";
    exit 1;}

This is the output in the commandline

call: D:\tools\PacketCreationTool/bin/perl/winx64/bin/perl.exe D:/tools/PacketCr
eationTool/scripts/ContentCheck.pl -t "C:/Documents and Settings/pkkonath/Deskto
p/saved/myMockName.TGZ" -b "input file/myMockName.TGZ" -o myMockName.validate -s
 10

target C:/Documents

in base
*** Error:  Invalid command line option.  Use option -h  for help.

Any suggestions are welcome! Thanks.

2)When i pass it this way

my $call = "$perlExe $contentcheck -t \"$target_path\" -b \"$base_path\" -o $output_path -s $size_threshold";
print "\ncall: ".$call."\n";
system($call);

This is the output

call: D:\tools\PacketCreationTool/bin/perl/winx64/bin/perl.exe D:/tools/PacketCr
eationTool/scripts/ContentCheck.pl -t ""C:/Documents and Settings/pkkonath/Deskt
op/saved/myMockName.TGZ"" -b ""input file/myMockName.TGZ"" -o myMockName.validat
e -s 10

target C:/Documents

in base
*** Error:  Invalid command line option.  Use option -h  for help.

3)

my $call = "$perlExe, $contentcheck, '-t', $target_path, '-b', $base_path, '-o', $output_path, '-s', $size_threshold";
print "\ncall: ".$call."\n";
system($call);

This is the output:

Can't open perl script ",": No such file or directory
+7  A: 

Use the multi-argument form of system instead. This will save you from having to deal with shell-escaping issues, and is therefore a lot safer.

system( $perlExe, $contentcheck, '-t', $target_path, '-b', $base_path, '-o', $output_path, '-s', $size_threshold );
friedo
Can't open perl script ",": No such file or directory; This is the error i am getting now.
superstar
@superstar Did you copy and paste friedo's code, or do you have something like `system("$perlExe, $contentcheck, ...")`?
Greg Bacon
@gbacon: i used different options as pointed out in the question as 1) 2) 3)
superstar
@superstar Your 3) is closest to friedo's good advice but not the same.
Greg Bacon
@gbacon: i tried exactly the same as suggested by friedo and it gives the error as 2)
superstar
superstar, you're not. Your argument is a string, friedo's argument is a list. The difference is crucial.
daxim
+3  A: 

Since you say "I pass them in quotes," I take it you mean that you are passing these paths into your script via the shell. If that's so, then the shell is using your quotes to determine the arguments - it doesn't take the quotes themselves to be part of the argument.

For instance, take the following simple script:

echo $1

And note the difference between these various argument formats:

# ./test.sh 1 2 3
1

# ./test.sh "1 2 3"
1 2 3

# ./test.sh "\"1 2 3\""
"1 2 3"

If you really want to pass quotes into your script from the command line, you have to escape them, as in the third example.

That's not a very user-friendly option, though, so I recommend you add the quotes to all full paths in your script. Eg. -t "$target_path", and so forth.

ladenedge
A: 

Escape it means:

system("cmd -a \"$firstopt\" -b \"$secondopt\"");

;)

3ED
A: 

Is Contentcheck.pl a program you have written. It would seem more natural to me to make it a subroutine (if you only want to use it from this program) or a module if you need to use it from elsewhere.

Then the paramters become argumenst to the subroutine and you don't need to worry about spaces etc.

justintime
contentcheck.pl is a script that can run by itself(for other functionalities). So i have to use it by passing the parameters.
superstar