home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / unix / shell / 4796 < prev    next >
Encoding:
Text File  |  1992-11-19  |  2.2 KB  |  93 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!tellab5!seong
  3. From: seong@tellabs.com (Scott Seong)
  4. Subject: pushd, popd, dirs for ksh
  5. Message-ID: <1992Nov19.230627.27426@tellab5.tellabs.com>
  6. Followup-To: seong@tellabs.com
  7. Sender: news@tellab5.tellabs.com (News)
  8. Nntp-Posting-Host: sunph
  9. Organization: Tellabs, Lisle,IL
  10. Date: Thu, 19 Nov 1992 23:06:27 GMT
  11. Lines: 80
  12.  
  13. I am a recent switch-over from tcsh to ksh.  Since then, I missed useful
  14. csh functions.  The following ksh functions might be used to emulate pushd,
  15. popd, and dirs.
  16.  
  17. ###
  18. ### Built-in csh functions for ksh.
  19. ### Written by Scott Seong
  20. ###
  21. function dirs {
  22.     DSTK=${DSTK:-'$PWD':}; export DSTK
  23.     if [ "$(whoami)" = "root" ]
  24.     then
  25.                 eval echo "${DSTK%:}" |sed 's;:; ;g'
  26.     else
  27.         eval echo "${DSTK%:}" |sed -e "s;$HOME;~;g" -e 's;:; ;g'
  28.     fi
  29.     }
  30.  
  31. function pushd {
  32.     export DSTK=${DSTK:-'$PWD':}
  33.     case $# in
  34.     (0)    REMD=${DSTK#*:}
  35.         if [ -z "$REMD" ]; then
  36.             echo >&2 "Error: (pushd) No other directory."
  37.             return
  38.         else
  39.             TOPD=${REMD%%:*}
  40.             SAVD=$(pwd)
  41.             cd $TOPD && DSTK='$PWD':$SAVD:${REMD#*:}; export DSTK
  42.             dirs
  43.         fi ;;
  44.  
  45.     (1)    if [ "$(expr $1 : '\(.\).*')" = "+" ]; then    # pushd +n
  46.             let NUMB=$(expr $1 : '+\(.*\)')
  47.             let ACTL=$(echo $DSTK |/bin/awk -F: '{print NF}')-1
  48.             if (( NUMB == 0 )); then
  49.                 echo >&2 "Error: (pushd) $1 does not exist"
  50.                 return
  51.             elif (( NUMB >= ACTL )); then
  52.                 echo >&2 "Error: (pushd) Directory stack not deep enough"
  53.                 return
  54.             else
  55.                 SAVD=$(pwd)
  56.                 DSTK=$(echo "$SAVD:${DSTK#*:}" |/bin/awk -F: '{
  57.                     for (i=1; i<NF; i++)
  58.                         stk[i] = $i
  59.                     }
  60.                     END {
  61.                         for (j=INDEX+1; j<i; j++)
  62.                             printf ("%s:",stk[j]);
  63.                         for (j=1; j<=INDEX; j++)
  64.                             printf ("%s:",stk[j]); 
  65.                         print
  66.                     }' INDEX=$NUMB - )
  67.                 cd ${DSTK%%:*} && export DSTK='$PWD':${DSTK#*:}
  68.             fi
  69.         else
  70.             SAVD=$(pwd)
  71.             cd $1 && export DSTK='$PWD':$SAVD:${DSTK#*:}
  72.         fi
  73.         dirs ;;
  74.     (*)    echo >&2 "Usage: popd [New_Dir]" ;;
  75.     esac
  76.     }
  77.  
  78. function popd {
  79.     DSTK=${DSTK:-'$PWD':}; export DSTK
  80.     let ACTL=$(echo $DSTK |/bin/awk -F: '{print NF}')-1
  81.     if (( ACTL == 1 ))
  82.     then
  83.         echo >&2 "Error: (popd) Directory stack empty."
  84.     else
  85.         DSTK=${DSTK#*:}
  86.         cd ${DSTK%%:*} && export DSTK='$PWD':${DSTK#*:}
  87.         dirs
  88.     fi
  89.     }
  90. -- 
  91. Scott Seong
  92. seong@tellabs.com
  93.