Browse Source

Initial version

George Jones 2 years ago
commit
cb1d0181ee
2 changed files with 45 additions and 0 deletions
  1. 1 0
      README.org
  2. 44 0
      lib/bash/bashutils.sh

+ 1 - 0
README.org

@@ -0,0 +1 @@
+This contains my common bash functions and other bash related things.

+ 44 - 0
lib/bash/bashutils.sh

@@ -0,0 +1,44 @@
+# These are common functions and settings I use when writing things in bash
+#
+# Usage: source $HOME/lib/bash/bashutils.sh
+set -e; set -u
+
+ARGV=("$@")
+ARGC=("$#")
+#ARGV=("${ARGV[@]:1}") # shift ARGV
+#ARGC=${#ARGV[@]} # get count
+
+# Helper functions
+
+
+# TODO improve use of func
+#      - "bash"        ::  if just called from shell
+#      - $0            ::  if just in the name body of a script
+#      - $0:$FUNCNAME  ::  if in a function
+
+if [[ -v 0 ]]; then
+    if [[ "$0" == "-bash" ]]; then
+        PROG="bash"
+    else
+        PROG=`basename "$0" | tr -d '\n'`
+    fi
+else
+    PROG="unknown"
+fi
+
+
+function info()  { echo `date +%c` ${PROG}\: info: "$@" 1>&2; }
+function warn()  { echo `date +%c` ${PROG}\: warning: "$@" 1>&2; }
+function error() { echo `date +%c` ${PROG}\: error: "$@" 1>&2; }
+# Temporary debug messages
+#   DEBUG=1 debug foo
+#
+# GLobal debug messages
+#   export DEBUG=1
+#   ...
+#   debug foo
+#
+# TODO
+#   have it check debug levels and or strings
+function debug() { [[ -v DEBUG ]] && echo `date +%c` ${PROG}\: debug: "$@" 1>&2 || true ; }
+function die()   { echo `date +%c` ${PROG}\: fatal: "$@" 1>&2 && exit 1; }