在Perl中使用空格执行Windows命令 [英] Execute Windows command with spaces in Perl
本文介绍了在Perl中使用空格执行Windows命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的firefox安装在 C:\ Program Files \ Mozilla Firefox 中.
My firefox is installed in C:\Program Files\Mozilla Firefox.
我是这样尝试阅读 firefox 版本的:
I amm trying to read the firefox version like this:
open (IN, "\"C:\\Program\ Files\\Mozilla\ Firefox\\firefox.exe\" --version|") or die "Couldn't fork - $!\n";
my $aarray = (IN);
print $fh "=========Array Content========> $aarray <====\n";
close(IN);
这是完美的工作.但是,当我将路径或命令放在变量中并尝试执行时,它无法显示无法分叉"
This is working perfectly. But, when I put path or command in a variable and try to execute, it fails saying "Couldn't fork"
my $ffox = 'C:\Program Files\Mozilla Firefox\firefox.exe';
$ffox =~ s/\\/\\\\/g; #Replacing single \ with double \\
$ffox =~ s/\ /\\ /g; # Adding escape character before space
chop($firefoxVer); # remove last \n chr.
$ffox =~ s/$/\\\"/g; # With the below two commands, massaging the command to become
$ffox =~ s/^/\\\"/g; # "C:\\Program\ Files\\Mozilla\Firefox\\firefox.exe\"
$firefoxVer = $ffox.' --version|'; # Adding "version|" to the above command
$firefoxVer =~ s/$/\"/g; # and make it look like:
$firefoxVer =~ s/^/\"/g; # "\"C:\\Program\ Files\\Mozilla\\Firefox\\firefox.exe\" --version|"
open (IN, $firefoxVer) or die "Couldn't fork - $!\n"; ==> Fails
推荐答案
使用反引号可以直接获得结果,而无需open
.引用需要保留空格的命令行内容:
With the backticks you can get the result directly without the open
. Quote the command-line things that need to preserve their spaces:
use v5.10;
my $version = `"C:\\Program Files\\Mozilla Firefox\\firefox.exe" -version`;
say "version is $version";
如果该目录在您的路径中,则无需四处逛逛:
If that directory is in your path, you don't need to fool around with that:
my $firefox = 'C:\\Program Files\\Mozilla Firefox';
$ENV{PATH} .= ";$firefox";
my $version = `firefox -version`;
这篇关于在Perl中使用空格执行Windows命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文