bashutils.sh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # These are common functions and settings I use when writing things in bash
  2. #
  3. # Usage: source $HOME/lib/bash/bashutils.sh
  4. set -e; set -u
  5. ARGV=("$@")
  6. ARGC=("$#")
  7. #ARGV=("${ARGV[@]:1}") # shift ARGV
  8. #ARGC=${#ARGV[@]} # get count
  9. # Helper functions
  10. # TODO improve use of func
  11. # - "bash" :: if just called from shell
  12. # - $0 :: if just in the name body of a script
  13. # - $0:$FUNCNAME :: if in a function
  14. if [[ -v 0 ]]; then
  15. if [[ "$0" == "-bash" ]]; then
  16. PROG="bash"
  17. else
  18. PROG=`basename "$0" | tr -d '\n'`
  19. fi
  20. else
  21. PROG="unknown"
  22. fi
  23. function info() { echo `date +%c` ${PROG}\: info: "$@" 1>&2; }
  24. function warn() { echo `date +%c` ${PROG}\: warning: "$@" 1>&2; }
  25. function error() { echo `date +%c` ${PROG}\: error: "$@" 1>&2; }
  26. # Temporary debug messages
  27. # DEBUG=1 debug foo
  28. #
  29. # GLobal debug messages
  30. # export DEBUG=1
  31. # ...
  32. # debug foo
  33. #
  34. # TODO
  35. # have it check debug levels and or strings
  36. function debug() { [[ -v DEBUG ]] && echo `date +%c` ${PROG}\: debug: "$@" 1>&2 || true ; }
  37. function die() { echo `date +%c` ${PROG}\: fatal: "$@" 1>&2 && exit 1; }