本文介绍了将参数传递给带有空格的目录的命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从perl进行ContentCheck.pl的系统调用,并将参数传递给目录(具有空格).因此,我用引号将它们传递,但它们并未在ContentCheck.pl文件中被选中
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.pm1)
Random.pm1)
my $call = "$perlExe $contentcheck -t $target_path -b $base_path -o $output_path -s $size_threshold";
print "\ncall: ".$call."\n";
system($call);
Contentcheck.pl
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
推荐答案
请改用 system
的多参数形式.这样可以避免处理外壳转义问题,因此更加安全.
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 );
这篇关于将参数传递给带有空格的目录的命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!