Created
April 29, 2017 20:01
-
-
Save dreamcat4/b56fde008c738791c3435220b86b6d04 to your computer and use it in GitHub Desktop.
Change a Unix path in to Windows path for WSL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
winpath() { | |
# get the Windows user path, convert to Unix line endings | |
user_path=$(echo "$(/mnt/c/Windows/System32/cmd.exe /C echo %HOMEDRIVE%%HOMEPATH%)" | tr -d "\r") | |
# expand the specified path | |
target_path=$(readlink -f $1) | |
# change ~ to $user_path | |
if grep -q "^/home/" <<< $target_path; then | |
temp_user_path=$(echo "$user_path" | sed -e 's|\\|/|g' -e 's|^\([A-Za-z]\)\:/\(.*\)|/mnt/\L\1\E/\2|' -e 's|^M$||') | |
# if there was something after ~, add it to the end of the $user_path | |
if grep -q "^/home/\(.*\)/\(.*\)" <<< $target_path; then | |
target_path=$temp_user_path$(echo "$target_path" | sed -e 's|^/home/*/\(.*\)|/\2|') | |
# if there was nothing after ~, $target_path is $user_path | |
else | |
target_path=$temp_user_path | |
fi | |
fi | |
# check if a Windows path is getting parsed | |
if grep -q "^/mnt/[a-z]" <<< $target_path; then | |
# swap /mnt/[a-z] with [A-Z]: and / with \ | |
echo $(echo "$target_path" | sed -e 's|^\(/mnt/\([a-z]\)\)\(.*\)|\U\2:\E\3|' -e 's|/|\\|g') | |
else | |
# return the user's home directory if a Unix path was parsed | |
echo $user_path | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment