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条)