Shell script to take windows path as argument to change directory - Stack Overflow

I wrote a simple shell script to change path where folder path is provided in Windows format (it would

I wrote a simple shell script to change path where folder path is provided in Windows format (it would be very handy to copy/paste path from windows and provide it to unix cd).

Here is the script

cdwin() {
echo "args = " $@
echo "first arg = " $1
win_path="$1"
linux_path=$(echo "$win_path" | sed 's|\\|/|g')
linux_path="${linux_path:2}"

echo "Windows path = $win_path"
echo "Linux path   = $linux_path"

# Change to the converted directory and check for success
if cd "$linux_path"; then
    echo "Changed directory to: $linux_path"
else
    echo "Failed to change directory to: $linux_path"
fi
}

This function when written as a shell script was working fine - but actual path did not change - since when the script exits, it reverts to parent path (I used pwd to verify the script was doing its job).

Since, I wanted the path change to be retained after the script exits, I put it as a function inside my .kshrc file.

Now, when I run the script, I am not able to escape backslashes, and as a result, the script does not work. Here is the output when I run the following command.

cdwin "R:\a\b\c\d\e\f\g" 

Here is the output

args =  R\d
       gfirst arg =  R\d
                        gWindows path = R\d
                                           gLinux path   =/d

ksh: cd: /home/jzsn3m/d
                   : No such file or directory
Failed to change directory to:/d

My assumption is, the script is not getting the input argument correctly. Using single quotes also does not work.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745350389a4623796.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信