Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!seismo!lll-lcc!mordor!sri-spam!sri-unix!hplabs!hpcea!hpfcdc!hpfcdt!ajs From: ajs@hpfcdt.UUCP Newsgroups: net.sources Subject: Re: Korn Shell LRU Directory History Message-ID: <7930001@hpfcdt.HP.COM> Date: Mon, 30-Mar-87 18:51:31 EST Article-I.D.: hpfcdt.7930001 Posted: Mon Mar 30 18:51:31 1987 Date-Received: Sat, 4-Apr-87 06:22:44 EST References: <337@nud.UUCP> Organization: HP Fort Collins, CO Lines: 141 # Here's yet another way to remember and re-use old directories. This one # remembers everywhere you've been during a login session, and lets you # pick from a sorted menu ("mcd") or list a Pareto-style summary ("lcd"). # I've found it only occasionally useful, but fast and painless. If # nothing else you might find these functions educational. # This is a shell archive. Remove anything before this line, # then unpack it by saving it in a file and typing "sh file". # # Wrapped by ajs at hpfcdt on Mon Mar 30 16:49:13 1987 # # This archive contains: # ksh.cd # # Error checking via wc(1) will be performed. echo x - ksh.cd cat >ksh.cd <<'@EOF' # ksh functions to remember directory changes and let you pick (from a menu) # directories to which to cd. # Usage: # Read this file with ". file", or put it in your .profile or .kshrc. Then # cd as usual. Say "mcd" to get a menu of places you've been, to pick from. # Say "lcd" to list your cd history, most-visited first. # Warning: Some things don't work right due to ksh deficiencies: # # - The typeset -x below is intended to cause DIRHIST[] to be exported to # subshells. But ksh only exports the first element of any array (and # that's clobbered when mycd() is declared). Too bad. # # - For some reason, the quotes around "$@" in the select in mcd() cause menu # item 1 to be munged sometimes, reason unknown. Remove the quotes if this # is a problem and you never visit a directory with a blank or tab in its # name. # INITIALIZE: # # Note that during .profile, $PWD is not set yet. if [ -z "${DIRHIST:-}" ] # not already set. then DIRHIST="${PWD:-$HOME}" fi typeset -x DIRHIST typeset -x DIRCOUNT=1 # just for fun, how often before. # FUNCTION mycd(): DO A CD AND REMEMBER ALL DIRS VISITED: alias cd=mycd # alias > built-in > function. mycd() { typeset -i dirat=0 # current saved dir index; local var. typeset -i dirs=${#DIRHIST[*]} # number of entries in "global" array. # Do the change: if 'cd' "${@-$HOME}" # call shell built-in. then # only if it succeeded. # See if new directory name (note, full name) is already known: while [ $dirat -lt $dirs ] do if [ "${DIRHIST[$dirat]}" = "$PWD" ] then break fi (( dirat=dirat + 1 )) done # Save new, or tell count for old: if [ $dirat -ge $dirs ] then DIRHIST[$dirat]="$PWD" DIRCOUNT[$dirat]=1 echo "(first time here)" else echo "(previous: ${DIRCOUNT[$dirat]})" (( DIRCOUNT[$dirat]=${DIRCOUNT[$dirat]} + 1 )) fi fi } # mycd(). # FUNCTION mcd(): DO A CD CHOOSING FROM A MENU: mcd() { set -- "${DIRHIST[@]}" # copy to positional parameters. set -s # sort them. (remove if not desired) select dir in "$@" # choose one. do if [ "${dir-}" ] # valid response. then echo "+ cd $dir" mycd "$dir" # use function for side-effects. else echo "cd: no change" fi break done } # mcd(). # FUNCTION lcd(): LIST CD HISTORY: lcd() { typeset -i dirat=0 # current saved dir index; local var. typeset -i dirs=${#DIRHIST[*]} # entries in "global" array. while [ $dirat -lt $dirs ] do echo "${DIRCOUNT[$dirat]}\t${DIRHIST[$dirat]}" (( dirat=dirat + 1 )) done | sort +0nr -1 } # lcd(). @EOF if test "`wc -lwc