|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +## @file shell_script_template.bash |
| 4 | +## @brief a quick sample script template |
| 5 | +## @details |
| 6 | +## This template gives us a few nice things to start with: |
| 7 | +## |
| 8 | +## 1. some Doxygen-style comments to start things off |
| 9 | +## 2. flags to protect us from undefined variables and failed commands |
| 10 | +## 3. a `SCRIPT_PATH` variable so we can reference where the script lives |
| 11 | +## 4. an error trap that prints the line where an error happens |
| 12 | +## 5. a stack dump when errors do happen |
| 13 | +## 6. a wrapper to allow us to source this script as if it was a library |
| 14 | +## 7. CLI parameter handling |
| 15 | +## 8. automagic help / usage generation |
| 16 | +## @author Wes Dean |
| 17 | + |
| 18 | + |
| 19 | +set -euo pipefail |
| 20 | + |
| 21 | +## @var SCRIPT_PATH |
| 22 | +## @brief path to where the script lives |
| 23 | +declare SCRIPT_PATH |
| 24 | +# shellcheck disable=SC2034 |
| 25 | +SCRIPT_PATH="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd -P)" |
| 26 | + |
| 27 | +## @var DEFAULT_WORD |
| 28 | +## @brief default value for the 'word' CLI parameter |
| 29 | +declare -i DEFAULT_WORD |
| 30 | +# shellcheck disable=SC2034 |
| 31 | +DEFAULT_WORD="hello" |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +## @fn die |
| 36 | +## @brief receive a trapped error and display helpful debugging details |
| 37 | +## @details |
| 38 | +## When called -- presumably by a trap -- die() will provide details |
| 39 | +## about what happened, including the filename, the line in the source |
| 40 | +## where it happened, and a stack dump showing how we got there. It |
| 41 | +## will then exit with a result code of 1 (failure) |
| 42 | +## @retval 1 always returns failure |
| 43 | +## @par Example |
| 44 | +## @code |
| 45 | +## trap die ERR |
| 46 | +## @endcode |
| 47 | +die() { |
| 48 | + printf "ERROR %s in %s AT LINE %s\n" "$?" "${BASH_SOURCE[0]}" "${BASH_LINENO[0]}" 1>&2 |
| 49 | + |
| 50 | + local i=0 |
| 51 | + local FRAMES=${#BASH_LINENO[@]} |
| 52 | + |
| 53 | + # FRAMES-2 skips main, the last one in arrays |
| 54 | + for ((i=FRAMES - 2; i >= 0; i--)); do |
| 55 | + printf " File \"%s\", line %s, in %s\n" "${BASH_SOURCE[i + 1]}" "${BASH_LINENO[i]}" "${FUNCNAME[i + 1]}" |
| 56 | + # Grab the source code of the line |
| 57 | + sed -n "${BASH_LINENO[i]}{s/^/ /;p}" "${BASH_SOURCE[i + 1]}" |
| 58 | + done |
| 59 | + exit 1 |
| 60 | +} |
| 61 | + |
| 62 | +trap die ERR |
| 63 | + |
| 64 | + |
| 65 | +## @par Example |
| 66 | +## @code |
| 67 | +## # set values from their defaults |
| 68 | +## word="${DEFAULT_WORD}" |
| 69 | +## |
| 70 | +## # process long options |
| 71 | +## for arg in "$@" ; do |
| 72 | +## shift |
| 73 | +## case "$arg" in |
| 74 | +## '--word') set -- "$@" "-w" ;; |
| 75 | +## '--help') set -- "$@" "-h" ;; |
| 76 | +## *) set -- "$@" "$arg" ;; |
| 77 | +## esac |
| 78 | +## done |
| 79 | +## |
| 80 | +## # process short options |
| 81 | +## OPTIND=1 |
| 82 | +## while getopts "w:h" opt ; do |
| 83 | +## case "$opt" in |
| 84 | +## 'w') word="$OPTARG" ;; |
| 85 | +## 'h') display_usage ; exit 0 ;; |
| 86 | +## *) echo "Invalid option" ; display_usage ; exit 1 ;; |
| 87 | +## esac |
| 88 | +## done |
| 89 | +## |
| 90 | +## shift "$((OPTIND - 1)) |
| 91 | +## |
| 92 | +## # Process positional arguments |
| 93 | +## for file in "$@" ; do |
| 94 | +## printf "%s" "$file" |
| 95 | +## done |
| 96 | +## @endcode |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +[[ "$0" == "${BASH_SOURCE[0]}" ]] && main "$@" |
| 102 | + |
0 commit comments