Cygwin: Perl script $ENV{PATH} call returns unix path. How do I change it?

不羁的心 提交于 2020-01-25 11:54:08

问题


I am calling a Perl script from cygwin. The issue that I am having is that when it calls $ENV{PATH}, the path is receives is a unix path. How do I change my shell so it returns a DOS path?


回答1:


Cygwin is a Unix-emulation environment for Windows. It wouldn't work unless $PATH was a Unix-style path.

If you want perl to give you a DOS path, use a DOS-compatible Perl, like Strawberry Perl. You can even run Strawberry Perl (or whatever Windows-build of perl you use) from Cygwin if you want to.




回答2:


It has nothing to do with the shell, so changing shell won't help, and there's nothing you can change in the shell to help.

>echo %PATH% & perl -E"say $ENV{PATH}"
c:\progs\cygwin\bin;...;C:\Windows\system32;...
/usr/bin:...:/cygdrive/c/Windows/system32:...

The purpose of cygwin is to allow unix program to be compiled on a Windows machine with as little changes as possible, which means the system calls must behave as if the program was executing on a unix machine, which means the PATH must look like a unix path.

If you had used a Windows build of Perl (such as ActivePerl or Strawberry Perl), you wouldn't have this problem.

If you wish to continue using a Cygwin build of Perl, you could use the Cygwin tool cygpath to convert the paths for you.

$ perl -E'
    my $cmd = q{IFS=: ; cygpath -w $PATH};
    chomp( my @paths = `$cmd` );
    say for @paths;
'
C:\progs\cygwin\home\ikegami\usr\perlbrew\bin
.
C:\progs\cygwin\home\ikegami\bin
C:\progs\cygwin\usr\local\bin
C:\progs\cygwin\bin
C:\progs\perl5163-ap1603\site\bin
C:\progs\perl5163-ap1603\bin
C:\bin
C:\Windows\system32
C:\Windows
...



回答3:


You can use cygpath and call bash with: (Looks like you have to set IFS.)

` IFS=:; cygpath --windows \$PATH`

Or unsetting IFS.

`IFS=;cygpath --windows --path \$PATH`

Or you could just do it with a few simple rules.

map { 
    # turn cygdrives to drive letter + colon
    s!^/cygdrive/(\w)/!\U$1:!;   

    # locate the cygwin relative paths to whatever it is on your system.
    s!^/!$CYGWIN_HOME/!;

    $_ = qq("$_") if m/[() ]/; # quote paths where necessary

    # You don't really need to switch the slashes for perl, Java
    # a lot of other multi-platform tools.
    s!/!\\!g;
} split /:/, $ENV{PATH}
;

Note that you might have to expand the character set in the match expression.




回答4:


I hope this helps, although maybe a bit late :)

Copy the PATH to a Cygwin-unrecognised variable and use that instead:

set MYPATH=%PATH%
perl -e 'print join("\n",split(";",$ENV{MYPATH}),"\n")'


来源:https://stackoverflow.com/questions/21917495/cygwin-perl-script-envpath-call-returns-unix-path-how-do-i-change-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!