dirstacklib 730 B

123456789101112131415161718192021222324252627
  1. #!/bin/sh
  2. # This small library implements basic functionallity similar to pushd and
  3. # popd found in bash, for those shells that do not provide it
  4. # The names are different so it works on the shells that provide it as well
  5. # without any conflict
  6. __DIRSTACK=""
  7. PushDir () {
  8. [ ! -d "$1" ] && echo "pushd: not a directory: '$1'" 1>&2 && return 1
  9. [ ! -x "$1" ] && echo "pushd: cannot chdir to: '$1'" 1>&2 && return 1
  10. __DIRSTACK=$(pwd)"
  11. ${__DIRSTACK}" &&
  12. cd "$1" &&
  13. return 0
  14. }
  15. PopDir () {
  16. [ ! -n "${__DIRSTACK}" ] && echo "popd: directory stack is empty" 1>&2 && return 1
  17. __OLDIR=$(echo -n "${__DIRSTACK}" | head -n 1 | tr -d '\n\r') &&
  18. __DIRSTACK=$(echo -n "${__DIRSTACK}" | tail -n +2) &&
  19. cd "${__OLDIR}" &&
  20. return 0
  21. }