home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8709 / 16 < prev    next >
Encoding:
Internet Message Format  |  1990-07-13  |  2.3 KB

  1. Path: uunet!seismo!sundc!pitstop!sun!amdcad!ames!ll-xn!husc6!spdcc!m2c!necntc!ncoast!allbery
  2. From: gwyn@brl-smoke.ARPA (Doug Gwyn )
  3. Newsgroups: comp.sources.misc
  4. Subject: Re: directory stack manipulation routines for bourne shell
  5. Keywords: pushd, popd, dirs ...
  6. Message-ID: <4766@ncoast.UUCP>
  7. Date: 29 Sep 87 20:27:07 GMT
  8. Sender: allbery@ncoast.UUCP
  9. Organization: Ballistic Research Lab (BRL), APG, MD.
  10. Lines: 93
  11. Approved: allbery@ncoast.UUCP
  12. X-Archive: comp.sources.misc/8709/16
  13.  
  14. In article <4563@ncoast.UUCP> rickers@RUTGERS.EDU@drexel.UUCP (Rick Wargo) writes:
  15. >        Having been  working  for  a  long  time  under  csh  and  being
  16. >accustomed  to  the commands pushd and popd, I was saddened to find that
  17. >these commands were not avaiable under  my  version  of  Microport  Unix
  18. >System  V (version 2.2).
  19.  
  20. Sure they are!  But you need to set them up yourself as shell functions.
  21. I use something like the following (actually mine is much more elaborate,
  22. supporting "myx" layer banners etc., and exploits our sh's "builtin"
  23. command to allow redefining "cd" as a function):
  24.  
  25. DIRSTACK=...    # for pushd, popd, swapd
  26. PREVDIR="$HOME"    # for backd
  27.  
  28. if [ -z "$HOST" ]
  29. then    HOST=`uname` export HOST
  30. fi
  31.  
  32. backd(){ ch "$PREVDIR"; echo `pwd`; }
  33.  
  34. # Following should be "cd", but you need "builtin" for that.
  35. ch(){
  36.     PREVDIR="$CWD"
  37.     if [ $# -lt 1 ]
  38.     then    cd
  39.     else    cd "$1"
  40.     fi
  41.     CWD=`pwd` export CWD
  42.     # exported so interactive subshells can ch $CWD to outwit symbolic links
  43.     if [ "$CWD" = "$HOME" ]
  44.     then    PS1="$HOST"
  45.     else    PS1="$HOST":`echo "$CWD" | sed -e "s!^$HOME!~!"`
  46.     fi
  47.     PS1="$PS1"'$ '
  48. }
  49.  
  50. dirs(){
  51.     if [ "$DIRSTACK" != "" ]
  52.     then
  53.         echo "$CWD $DIRSTACK"
  54.     fi
  55. }
  56.  
  57. popd(){
  58.     set $DIRSTACK
  59.     if [ $# -ge 2 ]
  60.     then    DIRSTACK="$*"
  61.         ch $1
  62.         set $DIRSTACK    # ch clobbered $*
  63.         shift
  64.         DIRSTACK="$*"
  65.     fi
  66.     set --
  67. }
  68.  
  69. pushd(){
  70.     DIRSTACK=$CWD" $DIRSTACK"
  71.     if [ $# -lt 1 ]
  72.     then    set $HOME
  73.     fi
  74.     if ch $1
  75.     then    echo $DIRSTACK
  76.     else    popd
  77.     fi
  78.     set --
  79. }
  80.  
  81. swapd(){
  82.     DIRSTACK=$CWD" $DIRSTACK"
  83.     set $DIRSTACK
  84.     if [ $# -ge 3 ]
  85.     then    DIRSTACK="$*"
  86.         ch $2
  87.         set $DIRSTACK    # ch clobbered $*
  88.         DIRSTACK="$1"
  89.         shift 2
  90.         DIRSTACK=$DIRSTACK" $*"
  91.         echo $DIRSTACK
  92.     else    shift
  93.         DIRSTACK="$*"
  94.         echo 'swapd: No previous directory' >&2
  95.         set --
  96.         return 1
  97.     fi
  98.     set --
  99. }
  100.  
  101. readonly backd ch dirs popd pushd swapd
  102.  
  103. if [ "$CWD" ]
  104. then    ch "$CWD"    # outwit symbolic links
  105. else    ch "`pwd`"
  106. fi
  107.