windows - perl if( -e "带空格路径的窗口){}

标签 windows perl path executable space

我在 Windows 上:-/在我的脚本中我有:

$ENV{'Powmig Path'}powermt

那给我:

C:\Program\ Files\EMC\PowerPath\powermt

如果我执行 if(-e $ENV{'Powmig Path'}powermt) 它不起作用。

我尝试用一​​些替换来改变我的路径\/

我也尝试添加更多双引号,但似乎没有任何效果:-(

例子:

#!/usr/bin/perl
use strict;
use warnings;
use File::Spec;

if($^O =~ m/^MSWin32$/){
    my $tmp = File::Spec->catdir($ENV{'Powmig Path'}, "powermt");
    if(-e "\"$tmp\""){
        print "powermt found\n";
    }else{
        print "No multipathing found \"$tmp\"\n";
    }
    $tmp =~ s/\\/\//g;
    if(-e "\"$tmp\""){
        print "powermt found\n";
    }else{
        print "No multipathing found \"$tmp\"\n";
    }
}else{
    print "Error: Unknow OS\n";
}
exit;

输出:

C:\Users\sgargasson\Desktop>perl test.pl
No multipathing found "C:\Program Files\EMC\PowerPath\powermt"
No multipathing found "C:/Program Files/EMC/PowerPath/powermt"

尝试不同的文件后,问题来自空间...

有人可以帮帮我吗?

致谢

最佳答案

您确实意识到您不能只在源代码中键入一个字符串,对吧?你需要引用它:

print "$ENV{'Powmig Path'}powermt";
...
if (-e "$ENV{'Powmig Path'}powermt") 

这将对变量进行插值,在本例中是来自散列 %ENV 的散列值,并将其与字符串 powermt 连接。

如果您尝试将一个字符串连接到一个变量,您首先需要引用它,然后使用 operator将其附加到变量:

my $string = $ENV{'Powmig Path'} . "powermt";
#                                ^--- concatenation operator

但是,如果您正在尝试构建路径,则可以使用适合该任务的模块,例如 File::Spec :

use strict;
use warnings;
use File::Spec;

my $path = File::Spec->catdir($ENV{'Powmig Path'}, "powermt");

关于windows - perl if( -e "带空格路径的窗口){},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21118633/

相关文章:

ruby-on-rails - Rails 3 中图像路径的完整 url

c++ - 在不引用 Windows 上的当前目录的情况下解析相对路径

perl - 同一个字符可以有 2 种不同的 UTF-8 编码吗?

linux - 如何知道用户当前打开了哪些文件?

node.js - Windows 上的 Node 错误 : "Source Path Too Long"

python - MSSQL 连接字符串 - 使用另一组凭据的 Windows 身份验证

html - Perl解析html后出现未初始化值错误

windows - powershell windows 8.1 中的 cURL : "A drive with the name ' localhost' does not exist"

c# - 从 C# 应用程序调用 Chrome

c++ - 互斥还是不互斥?