Perl 向带有空格的目录的命令行传递参数
我正在从perl对ContentCheck.pl进行系统调用,并通过目录(有空格)传递参数。因此,我用引号传递它们,但在ContentCheck.pl文件中不会提取它们 下午4点 (一) Contentcheck.plPerl 向带有空格的目录的命令行传递参数,perl,command-line,Perl,Command Line,我正在从perl对ContentCheck.pl进行系统调用,并通过目录(有空格)传递参数。因此,我用引号传递它们,但在ContentCheck.pl文件中不会提取它们 下午4点 (一) 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
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.
(三)
这是输出:
Can't open perl script ",": No such file or directory
使用多参数形式的
system
。这将使您不必处理shell转义问题,因此更加安全
system( $perlExe, $contentcheck, '-t', $target_path, '-b', $base_path, '-o', $output_path, '-s', $size_threshold );
既然您说“我用引号传递它们”,那么我认为您的意思是通过shell将这些路径传递到脚本中。如果是这样,那么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\u path”
,等等。转义它的意思是:
system("cmd -a \"$firstopt\" -b \"$secondopt\"");
是您编写的
Contentcheck.pl
程序。对我来说,把它变成一个子程序(如果你只想从这个程序中使用它)或者一个模块(如果你需要从其他地方使用它)似乎更自然
然后参数就变成了子程序的参数,您不需要担心空格等问题。无法打开perl脚本“,”:没有这样的文件或目录;这就是我现在遇到的错误。@superstar你是复制并粘贴了弗里多的代码,还是有类似于
系统($perlExe,$contentcheck,…)
?@gbacon:我使用了不同的选项,正如问题中指出的那样,1)2)3)@superstar你的3)最接近弗里多的好建议,但不一样。superstar,你不是。你的论点是一个字符串,弗里多的论点是一个列表。区别是至关重要的。contentcheck.pl是一个可以自行运行的脚本(用于其他功能)。所以我必须通过传递参数来使用它。这是什么?我不记得写这个了。:/应该是:system(“cmd”、“-a”、$opt1、-b”、$opt2)代码>为以下内容生成:'wird app name'-a'/path/此处带空格/example.txt'-b'/2$/txt.txt'
。
# ./test.sh 1 2 3
1
# ./test.sh "1 2 3"
1 2 3
# ./test.sh "\"1 2 3\""
"1 2 3"
system("cmd -a \"$firstopt\" -b \"$secondopt\"");