解决方案是从 MSYS 中创建一个指向 Strawberry Perl 可执行文件的符号链接 提示smaudet 以供他输入:
首先,删除或重命名 MSYS 安装附带的 Perl 可执行文件,如果有的话(OP 已经这样做了);例如:
mv /usr/bin/perl /usr/bin/perl.msys
mv /usr/bin/cpan /usr/bin/cpan.msys
然后创建一个指向 Strawberry Perl 可执行文件的符号链接:
ln -s /c/strawberry/perl/bin/perl.exe /usr/bin/perl
# Unfortunately, doing the same for `cpan` doesn't work directly, because
# Strawberry Perl's `cpan` executable is a *batch* file, `cpan.bat`, which
# cannot be directly invoked from MSYS.
# To invoke it from MSYS (assuming it is in the %PATH%):
# cmd /c 'cpan.bat ...'
# With an explicit path:
# cmd /c 'c:\strawberry\perl\bin\cpan.bat ...'
#
# Here's how to create a stub script that still allows invocation as
# `cpan`:
echo 'cmd /c "C:\strawberry\perl\bin\cpan.bat $*"'>/usr/bin/cpan && chmod +x /usr/bin/cpan
一旦/usr/bin/perl 符号链接到位,带有shebang 行#!/usr/bin/perl 和#!/bin/perl 的现有脚本将再次起作用(后者也起作用,因为/bin 和/usr/bin 在MSYS 中实际上是相同的位置) .
请注意,使用更灵活的 shebang 行 #!/usr/bin/env perl 编写的脚本不需要,因为env 将直接在路径中找到 Strawberry Perl 的 perl.exe .
一些背景:
MSYS 和 Cygwin 等 Unix 仿真环境不尊重 Windows 的 %PATHEXT% 变量来确定调用(非二进制)文件的可执行文件。换句话说:文件扩展名对于执行没有任何意义。
相反,他们只根据文件是否有 shebang 行:
- 如果有,则使用 shebang 行中指定的可执行文件。
- 如果没有,则使用默认的(类似 POSIX 的)shell
/bin/sh。
- 因此,尝试直接调用
*.bat 或*.cmd 文件失败,因为它们没有Unix shebang 行,因此由/bin/sh 而不是cmd.exe 执行。
与 Windows 不同,这也适用于完全没有文件扩展名的(可执行)文件。