[net.sources] pushd and popd for bourne shell

abcscnuk@csun.UUCP (02/06/87)

        Here are some shell functions to implement ``pushd'' and ``popd'' for
bourne shell.  They should behave much in the same way as the ``pushd'' and
``popd'' commands in csh.  Having gotten used to using them in csh, I couldn't
live without them, so I decided to write up shell functions that will behave
similarly.

        If you're unfamiliar with ``pushd'' and ``popd,'' here are brief
descriptions:

        pushd -- ``cd'' to a directory (if one is specified) and save the
		 previous PWD into a directory stack.   If no arguments are
		 given, it cd's you to the directory at the top of the
		 directory stack and puts present PWD at the top of the
		 stack  (Essentially switches between the present directory
		 and directory at top of directory stack).

	popd  -- pop the top of directory stack off and ``cd'' to it.

							Naoto Kimura
                //-n-\\					(csun!abcscnuk)
        _____---=======---_____
    ====____\   /.. ..\   /____====
  //         ---\__O__/---        \\	Enterprise... Surrender or we'll
  \_\                            /_/	send back your *&^$% tribbles !!!
----- cut here ----- cut here ----- cut here ----- cut here -----
DIRSTAK=""

dirs() {
   echo `pwd` $DIRSTAK
}

pushdir() {
   if test $# -eq 0
   then
      if test "$DIRSTAK" = ""
      then
         echo "directory stack empty."
      else
	 OLDPWD=`pwd`
         popdir
         DIRSTAK="$OLDPWD $DIRSTAK"
      fi
   else
      while test "$*" != ""
      do
	 OLDPWD=`pwd`
         if cd "$1"
         then
            DIRSTAK="$OLDPWD $DIRSTAK"
         fi
         shift
      done
   fi
}

listhead() {
   echo $1
}

listtail() {
   shift
   echo $*
}

popdir() {
   if test $# -ne 0
   then
      echo "No parameters allowed with popdir"
   else
      if test "$DIRSTAK" = ""
      then
         echo "directory stack empty."
      else
         TMP1=`listhead $DIRSTAK`
         DIRSTAK=`listtail $DIRSTAK`
         if test "$TMP1" != ""
         then
            cd "$TMP1"
         fi
      fi
   fi
}

pushd() {
   if pushdir $*
   then
      dirs
   fi
}

popd() {
   if popdir $*
   then
      dirs
   fi
}

abcscnuk@csun.UUCP (02/07/87)

	Sorry about the posting for those with bourne shell w/o shell
functions.  I forgot to mention that I was using System V.  Looking over
the man entry on a BSD system, I found that bourne shell doesn't support
shell functions, so I can guess that there are probably other systems
that won't support shell functions in bourne shell.  I guess that there
are quite a few people out there that will find my posting quite
useless..

							Naoto Kimura
                //-n-\\					(csun!abcscnuk)
        _____---=======---_____
    ====____\   /.. ..\   /____====
  //         ---\__O__/---        \\	Enterprise... Surrender or we'll
  \_\                            /_/	send back your *&^$% tribbles !!!