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

烈酒焚心 提交于 2019-12-30 08:53:25

问题


I'm porting a Linux script to Windows & MinGW, which accesses the Android phone through ADB.

Sometime I need to pass the Android's file path as ADB command line option.

However, when invoking the ADB.exe, MinGW translates it to Windows' path.

For example,

adb shell cat /proc/version

Is translated as follows, resulting "No such file or directory" error in Android.

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

I found double-quotation helps to prevent that.

adb shell "cat /proc/version"

But is there any global siwtches or env variables to prevent MinGW for this converstion ?

The MinGW I'm using came with the "Git for Windows" package.

EDITED : I also hit another scnario, I cannot work-around with the double quotation.

$ 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

回答1:


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

Yes. Use this environment variable:

MSYS_NO_PATHCONV=1

e.g.

MSYS_NO_PATHCONV=1 adb shell cat /proc/version

Beware: programs might not work properly they expect Windows paths.

To work around this you can use escaping as mentioned on the documentation page (look at the bottom):

adb shell cat //proc\version

Rule: first / of parameter is duplicated, rest / are replaced with \

Depending on escaping (e.g. in .sh scripts) used you might need to duplicate \ character:

adb shell cat //proc\\version

This way only parameters you wrote with extra / prefix will be passed without conversion to Windows paths.




回答2:


Just found starting the double-slash is the charm.

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



回答3:


Please, can we get the terminology right here? MinGW does no path translation, such as you describe; it is the MSYS build environment, provided by MinGW.org as a companion to MinGW, which does this, so I guess you are actually using the version of MSYS supplied with Git for Windows.

I'm pleased that you have found the magic bullet which works in your case, but please be aware that there are some corner cases in which this "double slash" trick doesn't suffice; if you run into one of these, you may wish to consider Cygwin as an alternative hosting shell on Windows.



来源:https://stackoverflow.com/questions/28533664/how-to-prevent-msys-to-convert-the-file-path-for-an-external-program

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