Walking Up Directories For Every ..
by FossilizedDaemon from LinuxQuestions.org on (#6NDFR)
Hello,
I am currently trying to implement a basic shell function that will Code:cd ../ for every .. I type into the shell. This is actually being aped from magicdots for zsh. I know the underlying logic, but am mainly running into trouble with actually interfacing with the shell itself. I do not know if that makes sense, so I will just get the code in hopes it helps other understand. Currently I have the following function.
Code:_walk() {
case "${1}" in
..*)
i=0
directories_to_walk=$(printf "%s" "${1}" | tr -d '/' | grep -o '..' | wc -l)
while [ "${i}" != "${directories_to_walk}" ];
do
if [ $(pwd) = '/' ]; then
return;
fi
cd ../
i=$((i+1))
done
;;
esac
}This runs perfectly, and in fact is POSIX compliant so you can use it in any POSIX shell, there is one issue I can't fix however. I do not want to have to type
Code:walk ../../I just want to be able to type
Code:../../I know being able to do this will depend on the shell (I am using yash), but I cannot find how to set this for the life of me. Does anybody know?
I am currently trying to implement a basic shell function that will Code:cd ../ for every .. I type into the shell. This is actually being aped from magicdots for zsh. I know the underlying logic, but am mainly running into trouble with actually interfacing with the shell itself. I do not know if that makes sense, so I will just get the code in hopes it helps other understand. Currently I have the following function.
Code:_walk() {
case "${1}" in
..*)
i=0
directories_to_walk=$(printf "%s" "${1}" | tr -d '/' | grep -o '..' | wc -l)
while [ "${i}" != "${directories_to_walk}" ];
do
if [ $(pwd) = '/' ]; then
return;
fi
cd ../
i=$((i+1))
done
;;
esac
}This runs perfectly, and in fact is POSIX compliant so you can use it in any POSIX shell, there is one issue I can't fix however. I do not want to have to type
Code:walk ../../I just want to be able to type
Code:../../I know being able to do this will depend on the shell (I am using yash), but I cannot find how to set this for the life of me. Does anybody know?