最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin14浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论