home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.shell
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!tellab5!seong
- From: seong@tellabs.com (Scott Seong)
- Subject: pushd, popd, dirs for ksh
- Message-ID: <1992Nov19.230627.27426@tellab5.tellabs.com>
- Followup-To: seong@tellabs.com
- Sender: news@tellab5.tellabs.com (News)
- Nntp-Posting-Host: sunph
- Organization: Tellabs, Lisle,IL
- Date: Thu, 19 Nov 1992 23:06:27 GMT
- Lines: 80
-
- I am a recent switch-over from tcsh to ksh. Since then, I missed useful
- csh functions. The following ksh functions might be used to emulate pushd,
- popd, and dirs.
-
- ###
- ### Built-in csh functions for ksh.
- ### Written by Scott Seong
- ###
- function dirs {
- DSTK=${DSTK:-'$PWD':}; export DSTK
- if [ "$(whoami)" = "root" ]
- then
- eval echo "${DSTK%:}" |sed 's;:; ;g'
- else
- eval echo "${DSTK%:}" |sed -e "s;$HOME;~;g" -e 's;:; ;g'
- fi
- }
-
- function pushd {
- export DSTK=${DSTK:-'$PWD':}
- case $# in
- (0) REMD=${DSTK#*:}
- if [ -z "$REMD" ]; then
- echo >&2 "Error: (pushd) No other directory."
- return
- else
- TOPD=${REMD%%:*}
- SAVD=$(pwd)
- cd $TOPD && DSTK='$PWD':$SAVD:${REMD#*:}; export DSTK
- dirs
- fi ;;
-
- (1) if [ "$(expr $1 : '\(.\).*')" = "+" ]; then # pushd +n
- let NUMB=$(expr $1 : '+\(.*\)')
- let ACTL=$(echo $DSTK |/bin/awk -F: '{print NF}')-1
- if (( NUMB == 0 )); then
- echo >&2 "Error: (pushd) $1 does not exist"
- return
- elif (( NUMB >= ACTL )); then
- echo >&2 "Error: (pushd) Directory stack not deep enough"
- return
- else
- SAVD=$(pwd)
- DSTK=$(echo "$SAVD:${DSTK#*:}" |/bin/awk -F: '{
- for (i=1; i<NF; i++)
- stk[i] = $i
- }
- END {
- for (j=INDEX+1; j<i; j++)
- printf ("%s:",stk[j]);
- for (j=1; j<=INDEX; j++)
- printf ("%s:",stk[j]);
- print
- }' INDEX=$NUMB - )
- cd ${DSTK%%:*} && export DSTK='$PWD':${DSTK#*:}
- fi
- else
- SAVD=$(pwd)
- cd $1 && export DSTK='$PWD':$SAVD:${DSTK#*:}
- fi
- dirs ;;
- (*) echo >&2 "Usage: popd [New_Dir]" ;;
- esac
- }
-
- function popd {
- DSTK=${DSTK:-'$PWD':}; export DSTK
- let ACTL=$(echo $DSTK |/bin/awk -F: '{print NF}')-1
- if (( ACTL == 1 ))
- then
- echo >&2 "Error: (popd) Directory stack empty."
- else
- DSTK=${DSTK#*:}
- cd ${DSTK%%:*} && export DSTK='$PWD':${DSTK#*:}
- dirs
- fi
- }
- --
- Scott Seong
- seong@tellabs.com
-