Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!mit-eddie!think!ames!ptsfa!dsp From: dsp@ptsfa.UUCP Newsgroups: comp.unix.questions Subject: Re: Korn Shell Message-ID: <2201@ptsfa.UUCP> Date: Mon, 16-Mar-87 21:00:43 EST Article-I.D.: ptsfa.2201 Posted: Mon Mar 16 21:00:43 1987 Date-Received: Wed, 18-Mar-87 01:24:57 EST References: <580@csun.UUCP> <5680@brl-smoke.ARPA> Reply-To: dsp@ptsfa.UUCP (David St.Pierre) Organization: Pacific * Bell, San Ramon, CA Lines: 229 Keywords: Korn shell ksh aliases parameters In article <5680@brl-smoke.ARPA> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) writes: >In article <580@csun.UUCP> aeusesef@csun.UUCP (Sean Eric Fagan) writes: >>I tried making a >>function called cd, but it didn't work, and an alias called cd did work, but >>I was unable to pass parameters into it. Any one have any suggestions? > >I don't have much experience with ksh, but I have plenty with the >SVR2 shell, which ksh is supposed to be almost entirely upward- >compatible with. Indeed, the SVR2 shell provided no way to >redefine builtins. We have fixed that in our version, with no >apparent ill effects, and use the feature heavily (especially for >"cd"). Along the way we added V8's "builtin" and "whatis" builtins. Here's part of my ENV file for ksh. I've honestly forgotten some of this. Order is important because the built-in is used in the function _cd and then the alias is applied later. Also a function to implement the V8 "whatis" - I guess it does because that's what the comment tells me :-) (I didn't write it) I have no idea what the "builtin" builtin does. And popd, pushd, dirs, probably thanks to someone on the net a few years ago. --------- _cd () { cd $1 > /dev/null; PS1="$PWD !> " ; } alias cd=_cd alias dirs='. $HOME/.functions;dirs' alias popd='. $HOME/.functions;popd' alias pushd='. $HOME/.functions;pushd' # whatis --- ksh function to implement the V8 whatis builtin function whatis { 'typeset' i_ foo_ # local variables 'typeset' -i var for i_ in $* do var=0 if 'set' | grep "^${i_}=" > /dev/null then 'set' | grep "^${i_}=" var=1 fi foo_="`'whence' -v ${i_}`" case "$foo_" in *built-in*) 'print' - builtin ${i_} ;; *function*) 'typeset' -f ${i_} ;; *exported*) 'print' - "alias -x '${i_}=${foo_##*alias for }" ;; *tracked*) 'print' - "alias -t '${i_}=${foo_##*alias for }'" ;; *alias*) 'print' - "alias '${i_}=${foo_##*alias for }'" ;; *not\ found) if ((! var)) then 'print' - "$foo_" fi ;; *) 'print' - "${foo_##*is }" ;; esac done return 0 } unalias pushd popd dirs swapdir # # The directory stack grows upward from 0. This is different from # the csh version where the top of the stack is always 0. # However, the direction of our stack is secretly reversed # when necessary to preserve the illusion of a stack with top # at 0. The idea is to make this program appear identical # to its counterpart in csh. # # invariant: dirstack[top] == $PWD # # From the csh man page... # # dirs # Prints the directory stack; the top of the stack is at # the left, the first directory in the stack being the # current directory. # # popd # popd +n # Pops the directory stack, returning to the new top # directory. With a argument `+n' discards the nth entry # in the stack. The elements of the directory stack are # numbered from 0 starting at the top. # # pushd # pushd name # pushd +n # With no arguments, pushd exchanges the top two elements # of the directory stack. Given a name argument, pushd # changes to the new directory (ala cd) and pushes the # old current working directory (as in csw) onto the # directory stack. With a numeric argument, rotates the # nth argument of the directory stack around to be the # top element and changes to it. The members of the # directory stack are numbered from the top starting at # 0. # let top=0 dirstack[top]=$PWD pushd () { dirstack[top]=$PWD # kludge to force . to top of stack case $# in 0) if (( top > 0 )) ; then let n=top-1 swapdir $n $top else echo No other directory. fi ;; 1) case $1 in +[1-9]*) n=$1 n=${n#+} if (( n > top )) ; then echo Directory stack not that deep. break else let n=top-n # "reverse" the stack swapdir $n $top fi unset n ;; *) if cd $1 ; then ((top=top+1)) dirstack[top]=$PWD dirs fi ;; esac ;; *) echo Too many arguments. ;; esac } popd () { dirstack[top]=$PWD # kludge to force . to top of stack case $# in 0) case $top in 0) echo Directory stack empty. ;; *) unset dirstack[top] let top=top-1 cd ${dirstack[top]} dirs ;; esac ;; 1) case $1 in +[1-9]*) n=$1 n=${n#+} if (( n > top )) ; then echo Directory stack not that deep. unset n break else let n=top-n # "reverse" the stack, and # shift the stack down one for all entries >= n while (( n < top )) do let m=n+1 dirstack[n]=${dirstack[m]} let n=n+1 done unset dirstack[top] m n let top=top-1 dirs fi ;; *) echo Bad directory. ;; esac ;; *) echo Too many arguments. ;; esac } dirs () { dirstack[top]=$PWD # kludge to force . to top of stack let n=top while (( n >= 0 )) do echo "${dirstack[n]} "\\c let n=n-1 done echo "" unset n } swapdir () { t=${dirstack[$1]} dirstack[$1]=${dirstack[$2]} dirstack[$2]=$t cd ${dirstack[top]} dirs unset t } -- David St. Pierre 415/823-6800 {ihnp4,lll-crg,ames,qantel,pyramid}!ptsfa!dsp It looks like it's going to go on ad infinitum for a while.