KSH question about past directories
Dale Farnsworth
df at nud.UUCP
Wed Jan 25 08:07:32 AEST 1989
Sumit Dongre (dongre at optilink.UUCP) writes:
> Is there a way to track/recover the last few directories in ksh-i.
No, but I added three options to cd via ksh functions.
1. cd -l Displays the most recently used 20 current directories.
2. cd -foostring Changes directory to the most recent directory whose
name contains the string foostring.
3. cd -8 Changes to the eighth most recent directory.
The directory list is maintained in most recently used order.
Here are the functions:
# If you want multiple shells to share the same history (either
# concurrently or serially, create a file and place its name in
# the shell variable CDHISTFILE.
alias cd=_cd
function _cd
{
typeset -i cdlen i status
typeset t
if [ $# -eq 0 ]
then
set -- $HOME
fi
t="$@"
if [ "$CDHISTFILE" -a -r "$CDHISTFILE" ]
then
typeset CDHIST=
set -- `<$CDHISTFILE`
((i=-1))
while [ $# -gt 0 ]
do
CDHIST[i=i+1]=$1
shift
done
fi
set -- $t
if [ "${CDHIST[0]}" != "$PWD" -a "$PWD" != "" ]
then
_cdins
fi
cdlen=${#CDHIST[*]}
case "$@" in
-)
if [ "$OLDPWD" = "" ] && ((cdlen>1))
then
print ${CDHIST[1]}
'cd' ${CDHIST[1]} ; status=$?
else
'cd' $@ ; status=$?
fi ;;
-l)
typeset -R3 num
((i=cdlen))
while (((i=i-1)>=0))
do
((num=i))
print "$num ${CDHIST[i]}"
done
return 0 ;;
-[0-9]|-[0-9][0-9])
if (((i=${1#-})<cdlen))
then
print ${CDHIST[i]}
'cd' ${CDHIST[i]} ; status=$?
else
'cd' $@ ; status=$?
fi ;;
-*)
t=${1#-}
((i=0))
while (((i=i+1)<cdlen))
do
case ${CDHIST[i]} in
*$t*) print ${CDHIST[i]}
'cd' ${CDHIST[i]} ; status=$?
break ;;
esac
done
if ((i>=cdlen)) ; then
'cd' $@ ; status=$?
fi ;;
*)
'cd' $@ ; status=$? ;;
esac
_cdins
if [ "$CDHISTFILE" ]
then
print -r ${CDHIST[*]} >$CDHISTFILE
fi
# set_prompt
return $status
}
function _cdins
{
typeset -i i
((i=-1))
while (((i=i+1)<${#CDHIST[*]}))
do
if [ "${CDHIST[$i]}" = "$PWD" ]
then
break
fi
done
if ((i>20))
then
((i=20))
fi
while (((i=i-1)>=0))
do
CDHIST[i+1]=${CDHIST[i]}
done
CDHIST[0]=$PWD
}
-Dale
--
Dale Farnsworth 602-438-3092 noao!asuvax!nud!df
More information about the Comp.unix.questions
mailing list