Windows cmd.exe命令传递的Perl系统函数在某些情况下需要双引号



Windows cmd.exe命令'type\Test\hello.txt'必须用双引号括起来,Perl system((函数才能正确执行。我没有在https://perldoc.perl.org/functions/system或https://perldoc.perl.org/perlport#system-在Windows上使用双引号的要求在任何地方都有记录吗

以下示例代码演示了在版本为2004(OS Build 19041.867(的Windows 10 Pro服务器上使用命令perl myperlperlsystest在Windows命令提示符窗口中运行时的问题,Perl版本为v5.26.3


$cmd='type Testhello.txt' ;   # Set Windows DOS command to be executed
system('ECHO '.$cmd) ;          # Display command to be executed
system($cmd) == 0               # Execute command - note "Testhello.txt not found" error but with $? == 0
or die "system("$cmd") failed: $?!" ;
if ($? == -1) {print "system("$cmd") failed to execute: $!!n"}
elsif ($? & 127) {printf "system("$cmd") child died with signal %d, %s coredumpn",
($? & 127),  ($? & 128) ? 'with' : 'without' ; exit}
elsif ($? != 0) {printf "system("$cmd") child exited with value %dn", $? ; exit}
$cmd='"'.$cmd.'"' ;             # Surround command string in double quotes
system('ECHO '.$cmd) ;          # Display command to be executed
system($cmd) == 0               # Execute command - note that command succeeds
or die "system("$cmd") failed: $?!" ;
if ($? == -1) {print "system("$cmd") failed to execute: $!!n"}
elsif ($? & 127) {printf "system("$cmd") child died with signal %d, %s coredumpn",
($? & 127),  ($? & 128) ? 'with' : 'without' ; exit}
elsif ($? != 0) {printf "system("$cmd") child exited with value %dn", $? ; exit}
$cmd='type Test10hello.txt' ; # Set Windows DOS command to be executed
system('ECHO '.$cmd) ;          # Display command to be executed -  note that "type Teshello.txt" is echoed
system($cmd) == 0               # Execute command - note "Testhello.txt not found" error but with $? == 0
or die "system("$cmd") failed: $?!" ;
if ($? == -1) {print "system("$cmd") failed to execute: $!!n"}
elsif ($? & 127) {printf "system("$cmd") child died with signal %d, %s coredumpn",
($? & 127),  ($? & 128) ? 'with' : 'without' ; exit}
elsif ($? != 0) {printf "system("$cmd") child exited with value %dn", $? ; exit}
system('"ECHO '.$cmd).'"' ;     # Display command to be executed (with ECHO command in double quotes)
$cmd='"'.$cmd.'"' ;             # Surround command string in double quotes
system($cmd) == 0               # Execute command
or die "system("$cmd") failed: $?!" ;
if ($? == -1) {print "system("$cmd") failed to execute: $!!n"}
elsif ($? & 127) {printf "system("$cmd") child died with signal %d, %s coredumpn",
($? & 127),  ($? & 128) ? 'with' : 'without' ; exit}
elsif ($? != 0) {printf "system("$cmd") child exited with value %dn", $? ; exit}

无论您给系统什么,都必须适用于将处理命令的东西。对原来问题的评论应该变成一个答案。

system文档指向exec,exec还表示:

当参数通过系统外壳执行时,结果会受到其怪癖和功能的影响。参见";CCD_ 3";详细信息请参阅perlop。

这也指向perlop中的backticks条目:

如何计算该字符串完全取决于系统上的命令解释器。在大多数平台上,如果您希望对shell元字符进行字面处理,则必须保护它们。这在实践中很难做到,因为不清楚如何逃离哪些角色。

Perl在这里不处理单个系统,因为它支持很多系统(即使使用这些系统的人都消失了(。


你在那里贴了一堵文字墙,我不得不重写它,看看你在做什么。与其重复相同的代码,子程序(具有一些合理的格式(对读者来说是礼貌的:(

my @commands = (
'type Testhello.txt',
q("type Testhello.txt")
'type Test10hello.txt',
);
foreach my $command ( @commands ) {
try_it( $command );
}
sub try_it  {
my( $cmd ) = @_;
system('ECHO '.$cmd); 
system($cmd) == 0               # Execute command
or die qq(system("$cmd") failed: $?!) ;
if ($? == -1) {
print qq(system("$cmd") failed to execute: $!!n);
}
elsif ($? & 127) {
printf qq(system("$cmd") child died with signal %d, %s coredumpn),
($? & 127),  ($? & 128) ? 'with' : 'without' ; 
}
elsif ($? != 0) {
printf qq(system("$cmd") child exited with value %dn), $? ; 
}
}

最新更新