Files
@ 372e0596bf33
Branch filter:
Location: majic-scripts/templates/TEMPLATE.sh.tpl
372e0596bf33
4.7 KiB
application/vnd.groove-tool-template
[TEMPLATE.sh.tpl] Switch to using upper-case names for global variables:
- Makes it easier to differntiate local variables in functions.
- Allows for avoiding "shadowing" of lower-cased/upper-cased variable
names by mistake in functions.
- Makes it easier to differntiate local variables in functions.
- Allows for avoiding "shadowing" of lower-cased/upper-cased variable
names by mistake in functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | # -*- mode: snippet -*-
# name: Bash script template with standard CLI processing and helper functions.
# key: bash-script
# contributor: Branko Majic <branko@majic.rs>
# type: snippet
# --
#!/bin/bash
#
# `(file-name-nondirectory (buffer-file-name))`
#
# Copyright (C) `(format-time-string "%Y")`, Branko Majic <branko@majic.rs>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Treat unset variables as errors.
set -u
PROGRAM="`(file-name-nondirectory (buffer-file-name))`"
VERSION="${1:version}"
function usage() {
cat <<EOF
\$PROGRAM \$VERSION, ${2:short description }
Usage:
\$PROGRAM [OPTIONS] ${3:arguments}
EOF
}
function short_help() {
cat <<EOF
\$(usage)
For more details see \$PROGRAM -h.
EOF
}
function long_help() {
cat <<EOF
\$(usage)
\$PROGRAM ${4:long description}
\$PROGRAM accepts the following options:
-q
Quiet mode.
-d
Enable debug mode.
-v
Show script version and licensing information.
-h
Show full help.
Please report bugs and send feature requests to <branko@majic.rs>.
EOF
}
function version() {
cat <<EOF
\$PROGRAM, version \$VERSION
+-----------------------------------------------------------------------+
| Copyright (C) `(format-time-string "%Y")`, Branko Majic <branko@majic.rs> |
| |
| This program is free software: you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation, either version 3 of the License, or |
| (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with this program. If not, see <http://www.gnu.org/licenses/>. |
+-----------------------------------------------------------------------+
EOF
}
# Set-up colours for message printing if we're not piping and terminal is
# capable of outputting the colors.
_COLOR_TERMINAL=\$(tput colors 2>&1)
if [[ -t 1 ]] && (( _COLOR_TERMINAL > 0 )); then
_TEXT_BOLD=\$(tput bold)
_TEXT_WHITE=\$(tput setaf 7)
_TEXT_BLUE=\$(tput setaf 6)
_TEXT_GREEN=\$(tput setaf 2)
_TEXT_YELLOW=\$(tput setaf 3)
_TEXT_RED=\$(tput setaf 1)
_TEXT_RESET=\$(tput sgr0)
else
_TEXT_BOLD=""
_TEXT_WHITE=""
_TEXT_BLUE=""
_TEXT_GREEN=""
_TEXT_YELLOW=""
_TEXT_RED=""
_TEXT_RESET=""
fi
# Set-up functions for printing coloured messages.
function debug() {
if [[ $DEBUG != 0 ]]; then
echo "\${_TEXT_BOLD}\${_TEXT_BLUE}[DEBUG]\${_TEXT_RESET}" "\$@"
fi
}
function info() {
if [[ $QUIET == 0 ]]; then
echo "\${_TEXT_BOLD}\${_TEXT_WHITE}[INFO] \${_TEXT_RESET}" "\$@"
fi
}
function success() {
if [[ $QUIET == 0 ]]; then
echo "\${_TEXT_BOLD}\${_TEXT_GREEN}[OK] \${_TEXT_RESET}" "\$@"
fi
}
function warning() {
echo "\${_TEXT_BOLD}\${_TEXT_YELLOW}[WARN] \${_TEXT_RESET}" "\$@" >&2
}
function error() {
echo "\${_TEXT_BOLD}\${_TEXT_RED}[ERROR]\${_TEXT_RESET}" "\$@" >&2
}
# Define error codes.
SUCCESS=0
ERROR_ARGUMENTS=1
# Disable debug and quiet modes by default.
DEBUG=0
QUIET=0
# If no arguments were given, just show usage help.
if [[ -z \${1-} ]]; then
short_help
exit "$SUCCESS"
fi
# Parse the arguments
while getopts "qdvh" opt; do
case "\$opt" in
q) QUIET=1;;
d) DEBUG=1;;
v) version
exit "$SUCCESS";;
h) long_help
exit "$SUCCESS";;
*) short_help
exit "$ERROR_ARGUMENTS";;
esac
done
i=\$OPTIND
shift \$(( i-1 ))
# Quiet and debug are mutually exclusive.
if [[ \$QUIET != 0 && \$DEBUG != 0 ]]; then
error "Quiet and debug options are mutually exclusive."
exit "\$ERROR_ARGUMENTS"
fi
|