如何防止MSYS为外部程序转换文件路径

How to prevent MSYS to convert the file path for an external program

我正在将 Linux 脚本移植到 Windows & MinGW,它通过 ADB 访问 Android phone。

有时我需要将 Android 的文件路径作为 ADB 命令行选项传递。

但是,当调用 ADB.exe 时,MinGW 将其转换为 Windows' 路径。

例如,

adb shell cat /proc/version

翻译如下,导致"No such file or directory"错误在Android.

adb shell cat C:/Program Files (x86)/Git/proc/version

我发现双引号有助于防止这种情况。

adb shell "cat /proc/version"

但是是否有任何全局 siwtches 或 env 变量来阻止 MinGW 进行此转换?

我使用的 MinGW 带有 "Git for Windows" 包。

已编辑:我还遇到了另一个问题,我无法使用双引号解决问题。

$ adb push test1.mp3 /data
failed to copy 'test1.mp3' to 'C:/Program Files (x86)/Git/data': No such file or directory

$ adb push test1.mp3 "/data"
failed to copy 'test1.mp3' to 'C:/Program Files (x86)/Git/data': No such file or directory

刚刚发现以双斜线开头是魅力所在。

https://web.archive.org/web/20201112005258/http://www.mingw.org/wiki/Posix_path_conversion

An argument starting with 2 or more / is considered an escaped Windows style switch and will be passed with the leading / removed and all \ changed to /.

Except that if there is a / following the leading block of /, the argument is considered to be a UNC path and the leading / is not removed.

| Argument from MSYS program | Sent to native Windows program as | Sent to native Windows program as
| //foobar                   | /foobar                           | double /  prevents conversion
| //foo\bar                  | /foo/bar                          | \  converted to /
| //foo/bar                  | //foo/bar                         | interpreted as UNC path, leading /  not removed

拜托,我们可以在这里找到术语吗? MinGW 不进行路径转换,如您所描述的;它是 MSYS 构建环境,由 MinGW.org 作为 MinGW 的伴侣提供,它执行此操作,所以我猜你实际上使用的是 [=20] 提供的 MSYS 版本=] 对于 Windows.

很高兴您找到了适用于您的情况的灵丹妙药,但请注意,在某些特殊情况下,此 "double slash" 技巧并不适用;如果您 运行 成为其中之一,您可能希望考虑将 Cygwin 作为 Windows.

上的替代托管 shell

But is there any global switches or env variables to prevent MinGW for this conversion ?

是的。使用此环境变量:

MSYS_NO_PATHCONV=1

例如

MSYS_NO_PATHCONV=1 adb shell cat /proc/version

注意:程序可能无法正常运行他们期望的 Windows 路径。

要解决此问题,您可以使用 documentation page(查看底部)中提到的转义:

adb shell cat //proc\version

规则:参数的第一个/是重复的,其余的/被替换为\

根据使用的转义(例如在 .sh 脚本中),您可能需要复制 \ 个字符:

adb shell cat //proc\version

这样只有参数 you 写有额外的 / 前缀将被传递而不转换为 Windows 路径。