Windows PATH to posix path conversion in bash

前端 未结 6 1545
北荒
北荒 2020-12-04 16:35

How can I convert a Windows dir path (say c:/libs/Qt-static) to the correct POSIX dir path (/c/libs/Qt-static) by

6条回答
  •  佛祖请我去吃肉
    2020-12-04 17:04

    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/^./\0:/'
    

    or more efficiently,

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

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

提交回复
热议问题