Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
993 views
in Technique[技术] by (71.8m points)

string - Windows PATH to posix path conversion in bash

How can I convert a Windows dir path (say c:/libs/Qt-static) to the correct POSIX dir path (/c/libs/Qt-static) by means of standard msys features? And vice versa?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

From Windows to 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,

echo "/$pth" | sed -e 's/\///g' -e 's/://'

From POSIX to 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/^./:/'

or more efficiently,

echo "$pth" | sed -e 's/^///' -e 's///\/g' -e 's/^./:/'

where $pth is a variable storing the Windows or POSIX path, respectively.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...