home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1877 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  3.1 KB

  1. From: jwc@Unify.Com (J. William Claypool)
  2. Newsgroups: comp.unix.shell,alt.sources
  3. Subject: Re: Pushd, popd, dirs for stock SysV.3 Bourne shell
  4. Message-ID: <fqp19ut@Unify.Com>
  5. Date: 27 Sep 90 19:51:30 GMT
  6.  
  7. In article <PCG.90Sep26164749@odin.cs.aber.ac.uk> pcg@cs.aber.ac.uk (Piercarlo Grandi) writes:
  8. >
  9. >You wwill find appended a small shell script that defines under a stock
  10. >System V Bourne shell the pushd, popd, dirs commands of csh(1). They are
  11. >trivially adapted to the Korn shell. I have taken care to make them as
  12. >robust as possible.
  13.  
  14. Well... since you mentioned it... Here is an implementation of directory
  15. stack for SysV Bourne shell which I've had floating around for a while.
  16. I took a similar approach.  I didn't make any attempt to handle space in a
  17. directory name.  I believe that it duplicates the full functionality of csh.
  18.  
  19. It also initializes the directory stack from $HOME/.dirstk if that file is
  20. present.
  21.  
  22. -------------------- cut here --------------------
  23. dirs(){
  24. case $# in
  25.     0)  echo `pwd` $dirstk |
  26.     sed -e "s;$HOME;~;g" -e 'y/\ /\\n/' |
  27.     nl -v0 ;;
  28.     1)  case $1 in
  29.         -l) echo `pwd` $dirstk ;;
  30.         *)  echo "usage: dirs [ -l ]" ;;
  31.     esac ;;
  32.     *)  echo "usage: dirs [ -l ]" ;;
  33. esac
  34. }
  35.  
  36. popd(){
  37. case $# in
  38.     0)  set $dirstk
  39.     case $# in
  40.         0)  echo "popd: Directory stack is empty." ;;
  41.         *)  if eval cd $1 ; then
  42.             shift
  43.             dirstk="$*"
  44.             dirs
  45.         fi
  46.     esac ;;
  47.     1)  case $1 in
  48.         +[0-9]|+[0-9][0-9])
  49.         cnt=`echo $1 | sed -e 's;+;;'`
  50.         set $dirstk
  51.         case $cnt in
  52.             0|00)
  53.             if eval cd $1 ; then
  54.                 shift
  55.                 dirstk="$*"
  56.                 dirs
  57.             fi ;;
  58.             *)  if [ $# -ge $cnt ] ; then
  59.                 svdirs=
  60.                 while [ $cnt -gt 1 ] ; do
  61.                 svdirs="$svdirs $1"
  62.                 shift
  63.                 cnt=`expr $cnt - 1`
  64.                 done
  65.                 shift
  66.                 dirstk="$svdirs $*"
  67.                 dirs
  68.             else
  69.                 echo "popd: Directory stack not that deep."
  70.             fi ;;
  71.         esac ;;
  72.         *)  echo "usage: popd [ +n ]" ;;
  73.     esac ;;
  74.     *)  echo "usage: popd [ +n ]" ;;
  75. esac
  76. }
  77.  
  78. pushd(){
  79. case $# in
  80.     0)  set $dirstk
  81.     case $# in
  82.         0)  ;;
  83.         *)  d1=`pwd`
  84.         if eval cd $1 ; then
  85.             shift
  86.             dirstk="$d1 $*"
  87.             dirs
  88.         fi ;;
  89.     esac ;;
  90.     1)  case $1 in
  91.         +[0-9]|+[0-9][0-9])
  92.         cnt=`echo $1 | sed -e 's;+;;'`
  93.         set $dirstk
  94.         if [ $# -ge $cnt ] ; then
  95.             svdirs=`pwd`
  96.             while [ $cnt -gt 1 ] ; do
  97.             svdirs="$svdirs $1"
  98.             shift
  99.             cnt=`expr $cnt - 1`
  100.             done
  101.             if eval cd $1 ; then
  102.             shift
  103.             dirstk="$* $svdirs"
  104.             dirs
  105.             fi
  106.         else
  107.             echo "pushd: Directory stack not that deep."
  108.         fi
  109.         ;;
  110.         +*) echo "usage: pushd [ +n | <dir> ]" ;;
  111.         *)  dirstk="`pwd` $dirstk"
  112.         if eval cd `echo $1 | sed -e "s;~;$HOME;"` ; then
  113.             dirs
  114.         else
  115.             set $dirstk
  116.             shift
  117.             $dirstk="$*"
  118.         fi ;;
  119.     esac ;;
  120.     *) echo "usage: pushd [ +n | <dir> ]" ;;
  121. esac
  122. }
  123.  
  124. svds(){
  125. dirs -l > $HOME/.dirstk
  126. }
  127.  
  128. if [ -s $HOME/.dirstk ] ; then
  129.     dirstk=`cat $HOME/.dirstk`
  130.     popd
  131. else
  132.     dirstk=
  133. fi
  134. -------------------- cut here --------------------
  135. -- 
  136.  
  137. Bill Claypool +1 916 920 1830 x341| I know what I know if you know what I mean
  138. jwc@Unify.Com                     |--------------------------------------------
  139.    ...!{csusac,pyramid}!unify!jwc |   SCCA SFR Solo II   74 es  1984 CRX 1.5
  140.