string Windows PATH 到 bash 中的 posix 路径转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13701218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows PATH to posix path conversion in bash
提问by Tomilov Anatoliy
How can I convert a Windowsdir path (say c:/libs/Qt-static
) to the correct POSIXdir path (/c/libs/Qt-static
) by means of standard msysfeatures? And vice versa?
如何通过标准msys功能将Windows目录路径(例如c:/libs/Qt-static
)转换为正确的POSIX目录路径(/c/libs/Qt-static
)?反之亦然?
回答by Rody Oldenhuis
I don't know msys
, but a quick google search showed me that it includes the sed
utility. So, assuming it works similar in msys
than it does on native Linux, here's one way how to do it:
我不知道msys
,但快速的谷歌搜索显示它包含该sed
实用程序。因此,假设它的工作方式与msys
本机 Linux 上的工作方式相似,这里是一种方法:
From Windows to POSIX
从 Windows 到 POSIX
You'll have to replace all backslashes with slashes, remove the first colon after the drive letter, and add a slash at the beginning:
您必须用斜杠替换所有反斜杠,删除驱动器号后的第一个冒号,并在开头添加一个斜杠:
echo "/$pth" | sed 's/\/\//g' | sed 's/://'
or, as noted by xaizek,
或者,正如 xaizek 所指出的,
echo "/$pth" | sed -e 's/\/\//g' -e 's/://'
From POSIX to Windows
从 POSIX 到 Windows
You'll have to add a semi-colon, remove the first slash and replace all slashes with backslashes:
您必须添加一个分号,删除第一个斜杠并将所有斜杠替换为反斜杠:
echo "$pth" | sed 's/^\///' | sed 's/\//\/g' | sed 's/^./echo "$pth" | sed -e 's/^\///' -e 's/\//\/g' -e 's/^./Output type options:
-d, --dos print DOS (short) form of NAMEs (C:\PROGRA~1\)
-m, --mixed like --windows, but with regular slashes (C:/WINNT)
-M, --mode report on mode of file (binmode or textmode)
-u, --unix (default) print Unix form of NAMEs (/cygdrive/c/winnt)
-w, --windows print Windows form of NAMEs (C:\WINNT)
-t, --type TYPE print TYPE form: 'dos', 'mixed', 'unix', or 'windows'
:/'
:/'
or more efficiently,
或更有效地,
sed '
\,/$, !s,$,/,
\,^/, s,/,:/,2
s,^/,,
s,/,\,g
' <<< "$@"
where $pth
is a variable storing the Windows or POSIX path, respectively.
其中$pth
是分别存储 Windows 或 POSIX 路径的变量。
回答by anishsane
Are you using it on cygwin? If yes, then there is a readymade utility called cygpath.exe
in cygwin package just for doing that.
你在cygwin上使用它吗?如果是,那么cygpath.exe
在 cygwin 包中有一个现成的实用程序,专门用于执行此操作。
/c/git
relative/dir
c:/git
~
.
..
/c
/c/
./relative/dir
/sd0/some/dir/
回答by hIpPy
Here is my implementation (tested on git bash).
这是我的实现(在 git bash 上测试)。
From POSIX to Windows
从 POSIX 到 Windows
/
<path with space>
Works for:
效劳于:
echo "$@" | sed ...
except
除了
$ MSYS_NO_PATHCONV=1 taskkill /F /T /IM ssh-agent.exe
Explanation:
解释:
\,^/, s,/,:/,2
(converts /drive/dir/
to /drive:/dir/
) is the heart of it and inserts :
before the 2
nd /
. I use ,
for delim instead of /
for readability. If starting with /
(\,^/,
), then replace /
with :/
for the 2
nd occurrence. I do not want to assume drive letter length of 1 so this works for /sd0/some/dir
.
\,^/, s,/,:/,2
( 转换/drive/dir/
为/drive:/dir/
) 是它的核心,并:
在2
nd之前插入/
。我使用,
delim 而不是/
为了可读性。如果在启动/
(\,^/,
),然后替换/
用:/
的2
第二发生。我不想假设驱动器号长度为 1,因此这适用于/sd0/some/dir
.
s,^/,,
removes the leading /
and s,/,\\,g
converts all /
to \
.
s,^/,,
删除前导/
和s,/,\\,g
将所有/
到\
。
\,/$, !s,$,/,
is to handle the corner case of /c
and ensure 2nd /
(/c/
) for the next command to work.
\,/$, !s,$,/,
是处理/c
2nd /
( /c/
)的极端情况并确保下一个命令起作用。
Note:
笔记:
If here string <<<
does not work in your shell then you can echo
and pipe as
如果这里的字符串<<<
在你的 shell 中不起作用,那么你可以用echo
管道作为
Errata
勘误表
Here e script
这里电子脚本
回答by Pod
The "correct" way in MSYS is:
MSYS 中的“正确”方式是:
##代码##This avoids having to manually translate slashes. It simply de-activates the path conversion.
这避免了必须手动翻译斜杠。它只是取消激活路径转换。