home *** CD-ROM | disk | FTP | other *** search
- From: barto@megatek.UUCP (David Barto)
- Newsgroups: comp.unix.shell,alt.sources
- Subject: Re: Pushd, popd, dirs for stock SysV.3 Bourne shell
- Message-ID: <750@tsunami.megatek.uucp>
- Date: 27 Sep 90 16:19:05 GMT
-
- In article <PCG.90Sep26164749@odin.cs.aber.ac.uk> pcg@cs.aber.ac.uk (Piercarlo Grandi) writes:
- >
- >You will find appended a small shell script that defines under a stock
- >System V Bourne shell the pushd, popd, dirs commands of csh(1). They are
- >trivially adapted to the Korn shell. I have taken care to make them as
- >robust as possible.
- >
-
- pushd, popd, and dirs --- written by Chris Bertin
- Pixel Computer Inc. ...!wjh12!pixel!pixutl!chris
- as modified by Patrick Elam of GTRI
- modified by david barto to allow 'pushd +n'
-
- cd is a function, which uses the 'builtin' keyword, available for
- those who have the BRL mods to the shell. (I think this can be changed
- to 'eval'.) It will look for PCDPATH and do chdirs down that path if
- the directory can be found on it.
-
- PCDPATH=". $HOME /burt/prj/common /burt/prj/sparc /archive2"
- export PCDPATH
- -----
-
- cd () {
- PREVDIR="$CWD"
- result=1
- if [ $# -lt 1 ] ; then
- builtin cd
- result=0
- elif [ -d $1 ] ; then
- builtin cd "$1"
- result=0
- else
- for i in $PCDPATH
- do
- nxtdir=$i/$1
- # echo "try $nxtdir"
- if [ $result != 0 -a -d $nxtdir ] ; then
- builtin cd "$nxtdir"
- result=0
- fi
- done
- unset nxtdir
- fi
- if [ $result != 0 ] ; then
- echo "$1: directory not found"
- else
- CWD=`pwd` export CWD
- set +u
- if [ "$CWD" = "$HOME" ]
- then
- PFX="$HOST"
- PS1=:~
- else
- PS1=`echo "$CWD" | sed -e "s!^$HOME!~!"` PFX="$HOST":
- fi
- umask 022
- echo "$CWD" | egrep -s "/burt/prj/common|/x"
- [ $? = 0 ] && umask 002
- if [ "$TERM" = "wy75" ]
- then
- wy75-title $PS1
- PS1="$PFX %e_ "
- elif [ "$TWM_RUNNING" = 1 ]
- then
- if [ "$TTY" != "" ]
- then
- twm-title "$TTY:$PFX$PS1"
- else
- twm-title "XX:$PFX$PS1"
- fi
- PS1="%e_ "
- else
- PS1="$PFX$PS1 %t%n%e_ "
- fi
- unset PFX
- set -u
- fi
- return $result
- }
-
- # pushd, popd, and dirs --- written by Chris Bertin
- # Pixel Computer Inc. ...!wjh12!pixel!pixutl!chris
- # as modified by Patrick Elam of GTRI
- # modified by david barto to allow 'pushd +n'
-
- pushd () {
- SAVE=`pwd`
- _DSTACK="$SAVE $_DSTACK"
- if [ "$1" = "" ]
- then
- if [ "$_DSTACK" = "$SAVE " ]
- then
- echo "pushd: directory stack empty."
- _DSTACK=""
- return 1
- fi
- set $_DSTACK
- cd $2
- set $_DSTACK
- shift 2
- _DSTACK="$SAVE $*"
- else
- case "$1" in
- +*)
- count=`echo $1 | sed 's/+//'`
- set $_DSTACK
- if [ $count -ge $# ] ; then
- echo "pushd: directory stack not that deep"
- shift
- _DSTACK=$*
- return 1
- fi
- TMP=
- while [ $count -gt 0 ]
- do
- TMP="$TMP $1"
- shift
- count=`expr $count - 1`
- done
- TD=$1
- shift
- _DSTACK="$TMP $*"
- unset TMP
- if [ -d $TD ]
- then
- cd $TD >&-
- unset TD
- else
- popd > /dev/null
- return 1
- fi
- ;;
- *)
- cd $1
- if [ $? -eq 1 ] ; then
- popd > /dev/null
- return 1
- fi
- ;;
- esac
- fi
- dirs
- return 0
- }
-
- popd () {
- if [ "$_DSTACK" = "" ]; then
- echo "popd: Directory stack empty"
- return 1
- fi
- case "$1" in
- +*)
- count=`echo $1 | sed 's/+//'`
- case "$count" in
- 0)
- echo "popd: Must have positive count"
- return 1
- ;;
- 1)
- popd
- return 0
- ;;
- *)
- _TMPSTK="`pwd` $_DSTACK"
- set $_TMPSTK
- if [ $count -gt $# ]; then
- echo "pushd: directory stack not that deep"
- return 1
- fi
- TMP=
- while [ $count -ge 1 ]
- do
- TMP="$TMP $1"
- shift
- count=`expr $count - 1`
- done
- shift # trash dir requested
- _DSTACK="$* $TMP"
- set $_DSTACK
- shift # trash 'pwd' from the top of the stack
- _DSTACK="$*"
- ;;
- esac
- ;;
- *)
- set $_DSTACK
- cd $1
- set $_DSTACK
- shift
- _DSTACK=$*
- ;;
- esac
- dirs
- return 0
- }
-
- dirs () {
- echo "`pwd` $_DSTACK"
- return 0
- }
-
- xchng () { # exchanged top two entries on the stack
- if [ "$_DSTACK" = "" ]
- then
- echo exchange directory stack empty
- return 1
- else
- pushd
- return 0
- fi
- }
- --
- David Barto uunet!megatek!barto barto@network.ucsd.edu
-