Pushd, popd, dirs for stock SysV.3 Bourne shell

J. William Claypool jwc at Unify.Com
Fri Sep 28 05:51:30 AEST 1990


In article <PCG.90Sep26164749 at odin.cs.aber.ac.uk> pcg at cs.aber.ac.uk (Piercarlo Grandi) writes:
>
>You wwill 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.

Well... since you mentioned it... Here is an implementation of directory
stack for SysV Bourne shell which I've had floating around for a while.
I took a similar approach.  I didn't make any attempt to handle space in a
directory name.  I believe that it duplicates the full functionality of csh.

It also initializes the directory stack from $HOME/.dirstk if that file is
present.

-------------------- cut here --------------------
dirs(){
case $# in
    0)  echo `pwd` $dirstk |
	sed -e "s;$HOME;~;g" -e 'y/\ /\\n/' |
	nl -v0 ;;
    1)  case $1 in
	    -l) echo `pwd` $dirstk ;;
	    *)  echo "usage: dirs [ -l ]" ;;
	esac ;;
    *)  echo "usage: dirs [ -l ]" ;;
esac
}

popd(){
case $# in
    0)  set $dirstk
	case $# in
	    0)  echo "popd: Directory stack is empty." ;;
	    *)  if eval cd $1 ; then
		    shift
		    dirstk="$*"
		    dirs
		fi
	esac ;;
    1)  case $1 in
	    +[0-9]|+[0-9][0-9])
		cnt=`echo $1 | sed -e 's;+;;'`
		set $dirstk
		case $cnt in
		    0|00)
			if eval cd $1 ; then
			    shift
			    dirstk="$*"
			    dirs
			fi ;;
		    *)  if [ $# -ge $cnt ] ; then
			    svdirs=
			    while [ $cnt -gt 1 ] ; do
				svdirs="$svdirs $1"
				shift
				cnt=`expr $cnt - 1`
			    done
			    shift
			    dirstk="$svdirs $*"
			    dirs
			else
			    echo "popd: Directory stack not that deep."
			fi ;;
		esac ;;
	    *)  echo "usage: popd [ +n ]" ;;
	esac ;;
    *)  echo "usage: popd [ +n ]" ;;
esac
}

pushd(){
case $# in
    0)  set $dirstk
	case $# in
	    0)  ;;
	    *)  d1=`pwd`
		if eval cd $1 ; then
		    shift
		    dirstk="$d1 $*"
		    dirs
		fi ;;
	esac ;;
    1)  case $1 in
	    +[0-9]|+[0-9][0-9])
		cnt=`echo $1 | sed -e 's;+;;'`
		set $dirstk
		if [ $# -ge $cnt ] ; then
		    svdirs=`pwd`
		    while [ $cnt -gt 1 ] ; do
			svdirs="$svdirs $1"
			shift
			cnt=`expr $cnt - 1`
		    done
		    if eval cd $1 ; then
			shift
			dirstk="$* $svdirs"
			dirs
		    fi
		else
		    echo "pushd: Directory stack not that deep."
		fi
		;;
	    +*) echo "usage: pushd [ +n | <dir> ]" ;;
	    *)  dirstk="`pwd` $dirstk"
		if eval cd `echo $1 | sed -e "s;~;$HOME;"` ; then
		    dirs
		else
		    set $dirstk
		    shift
		    $dirstk="$*"
		fi ;;
	esac ;;
    *) echo "usage: pushd [ +n | <dir> ]" ;;
esac
}

svds(){
dirs -l > $HOME/.dirstk
}

if [ -s $HOME/.dirstk ] ; then
    dirstk=`cat $HOME/.dirstk`
    popd
else
    dirstk=
fi
-------------------- cut here --------------------
-- 

Bill Claypool +1 916 920 1830 x341| I know what I know if you know what I mean
jwc at Unify.Com                     |--------------------------------------------
   ...!{csusac,pyramid}!unify!jwc |   SCCA SFR Solo II   74 es  1984 CRX 1.5



More information about the Alt.sources mailing list