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

  1. From: pcg@cs.aber.ac.uk (Piercarlo Grandi)
  2. Newsgroups: comp.unix.shell,alt.sources
  3. Subject: Pushd, popd, dirs for stock SysV.3 Bourne shell
  4. Message-ID: <PCG.90Sep26164749@odin.cs.aber.ac.uk>
  5. Date: 26 Sep 90 15:47:49 GMT
  6.  
  7.  
  8. You wwill find appended a small shell script that defines under a stock
  9. System V Bourne shell the pushd, popd, dirs commands of csh(1). They are
  10. trivially adapted to the Korn shell. I have taken care to make them as
  11. robust as possible.
  12.  
  13. -----------------------------cut here-------------------------------
  14. DIRS=''
  15.  
  16. # We do a few contortions to ensure correct behaviour
  17. # if directory pathnames contain spaces et similia.
  18. # The things that substitute ~ for $HOME are commented
  19. # out; you may want to comment them in.
  20.  
  21. cwd()
  22. {
  23.     pwd                # | sed 's|'"$HOME"'/|~/|'
  24. }
  25.  
  26. chdir()
  27. {
  28.     cd "$1"            # cd `echo "$1" | sed 's|^~/|'"$HOME"'/|`
  29. }
  30.  
  31. pushd()
  32. {
  33.     CWD="`pwd`"
  34.  
  35.     case "$1" in
  36.  
  37.     '')    case "$DIRS" in
  38.     '') echo "directory stack empty"; return 1;;
  39.     esac
  40.     set $DIRS; eval cd "$1"; >/dev/null || return 1
  41.     shift; DIRS="'$CWD' $@"; cwd;;
  42.  
  43.     *)    chdir "$1" || return 1
  44.     DIRS="'$CWD' $DIRS";;
  45.  
  46.     esac
  47.     return 0
  48. }
  49.  
  50. popd()
  51. {
  52.     case "$DIRS" in
  53.     '') echo "directory stack empty"; return 1;;
  54.     esac
  55.     set $DIRS; eval cd "$1"
  56.     shift; DIRS="$*"; cwd
  57.     return 0
  58. }
  59.  
  60. dirs()
  61. {
  62.     CWD="`pwd`" || return 1
  63.     eval echo "'$CWD' $DIRS"    # | sed 's|'"$HOME"'/|~/|g'
  64.     return 0
  65. }
  66.  
  67. ---------------------------cut here as well----------------------------
  68. --
  69. Piercarlo "Peter" Grandi           | ARPA: pcg%uk.ac.aber.cs@nsfnet-relay.ac.uk
  70. Dept of CS, UCW Aberystwyth        | UUCP: ...!mcsun!ukc!aber-cs!pcg
  71. Penglais, Aberystwyth SY23 3BZ, UK | INET: pcg@cs.aber.ac.uk
  72.