where.sh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # define where() function to tell where a command/alias/function is defined
  2. source $HOME/lib/bash/bashutils.sh # error,warning,...
  3. ARGV=("$@")
  4. ARGC=("$#")
  5. function where() ( # function in subshell, own namespece.
  6. export PROG="$FUNCNAMEo"
  7. ARG1="${1:-NONE}"
  8. ARG2="${2:-NONE}"
  9. function usage () {
  10. message=${1:-""}
  11. cat <<EOF 1>&2
  12. Usage: $0 [options] command
  13. Show WHERE a command is defined (which BINARY, alias FOO, function FOO)
  14. -h| --help print help text
  15. command this is the name of the command, alias, function, etc.
  16. Examples:
  17. where ls
  18. where myalias
  19. where myfunction
  20. EOF
  21. if [[ "${message}" != "" ]]; then
  22. echo 2>&1
  23. error "${message}"
  24. fi
  25. }
  26. case $ARG1 in
  27. NONE) usage "Missing argument. Need name of command." && return 1;;
  28. -d|--debug) DEBUG=1 && shift;;
  29. -h|--help) usage && return 1;;
  30. esac
  31. if [[ $# != 1 ]]; then
  32. usage "Expecting one arguemt, got $#: $* " && return 1;
  33. fi
  34. [[ -v DEBUG ]] && set -x
  35. case "`type -t $1`" in
  36. alias|file)
  37. type $1;
  38. ;;
  39. function)
  40. # Enable debugging
  41. #
  42. # shopt -s extdebug
  43. #
  44. # to get file names and line nmbers
  45. shopt -s extdebug
  46. shopt -q extdebug && declare -Ff $1 || echo $1 is a functon
  47. ;;
  48. *)
  49. type -t $1 || warn "$1 is not defined";
  50. ;;
  51. esac
  52. )
  53. function there() {
  54. where
  55. }