2010-04-09 126 views
2

我正在从perl为ContentCheck.pl进行系统调用,并传递带目录(具有空格)的参数。所以我通过他们的报价,但他们没有被拾起在ContentCheck.pl文件将参数传递给带有空格的目录的命令行

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;} 

这是输出在命令行中

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. 

欢迎任何建议! 谢谢。

2)当我通过这种方式

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

这是输出

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); 

这是输出:

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

回答

6

改为使用system的多参数形式。这将使您不必处理shell转义问题,因此更安全。

system($perlExe, $contentcheck, '-t', $target_path, '-b', $base_path, '-o', $output_path, '-s', $size_threshold); 
+0

无法打开Perl脚本“”:没有这样的文件或目录;这是我现在得到的错误。 – superstar 2010-04-09 15:40:45

+1

@superstar你复制并粘贴了friedo的代码,或者你有类似'system(“$ perlExe,$ contentcheck,...”)''的东西吗? – 2010-04-09 15:51:18

+0

@gbacon:我在问题中使用了不同的选项,如1)2)3) – superstar 2010-04-09 19:07:11

4

既然你说:“我通过他们的报价,”我想你的意思是要传递这些路径到脚本通过外壳。如果是这样,那么shell会使用你的引号来确定参数 - 它不会把引号本身作为参数的一部分。

例如,采取以下简单的脚本:

echo $1 

并注意这些不同的参数格式之间的区别:

# ./test.sh 1 2 3 
1 

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

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

如果你真的想引号传递到你的脚本命令行,你必须逃避它们,就像第三个例子。

虽然这不是一个非常用户友好的选项,所以我建议您将引号添加到脚本中的所有完整路径中。例如。 -t "$target_path"等等。

-2

逃避它的意思是:

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

Contentcheck.pl你写的程序。如果你需要从其他地方使用它,那么将它变成一个子程序(如果你只是想从这个程序中使用它)或者是一个模块,这似乎更自然。

然后paramters argumenst成为子程序,你不必担心空间等

+0

contentcheck.pl是一个可以自行运行的脚本(用于其他功能)。所以我必须通过传递参数来使用它。 – superstar 2010-04-09 20:11:37