perl if( -e "windows with space path){}

2019-07-12 21:02发布

I'm on Windows :-/ and in my script i've:

$ENV{'Powmig Path'}powermt

That give me:

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

if I do a if(-e $ENV{'Powmig Path'}powermt) it doesn't work.

I have try to change my path with some substitution \ /

I have also try to add more double quote but nothing seems to work :-(

Exemple:

#!/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;

Output:

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"

After some try with different files, the problem comming from the space...

Can somebody help me?

Thx in Adv

3条回答
孤傲高冷的网名
2楼-- · 2019-07-12 21:29

Here's a problem:

if(-e "\"$tmp\""){

You've got an extra set of quotes. The filename isn't "C:\Program Files\whatever", it's C:\Program Files\whatever. You only want those extra quotes if the filename is being interpreted by the Command Prompt, or something like that, and that's not the case here.

Try this instead, where I've removed the extraneous quotes ("\"$tmp\"" becomes "$tmp", which is exactly the same as $tmp):

if ( -e $tmp ) {
查看更多
在下西门庆
3楼-- · 2019-07-12 21:30

You do realize that you cannot just type a string into the source code, right? You need to quote it:

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

This will interpolate the variable, in this case a hash value from the hash %ENV, and concatenate it with the string powermt.

And if you do try to concatenate a string to a variable, you first need to quote it, and then use an operator to attach it to the variable:

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

If you are trying to build paths, though, you might use a module suitable for that task, such as File::Spec:

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

my $path = File::Spec->catdir($ENV{'Powmig Path'}, "powermt");
查看更多
4楼-- · 2019-07-12 21:34

Thanks a LOT to TLP

I'm so a stupid linux user!!!!

PROBLEM:

my $tmp="$ENV{'Powmig Path'}powermt";

SOLVE:

my $tmp="$ENV{'Powmig Path'}powermt.exe";

CORRECT CODE IS:

#!/usr/bin/perl
use strict;
use warnings;

if($^O =~ m/^MSWin32$/){
    if(-e "$ENV{'Powmig Path'}powermt.exe"){
        print "powermt found\n";
    }else{
        print "No multipathing found\n";
    }
}else{
    print "Error: Unknow OS\n";
}
exit;

I'm so stupid, I need to uncheck "Hide know extension" So many hours in this things...

查看更多
登录 后发表回答