-
Notifications
You must be signed in to change notification settings - Fork 1
/
bash-script-template.sh
executable file
·91 lines (75 loc) · 2.1 KB
/
bash-script-template.sh
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
#!/usr/bin/env bash
#
# So bash it is, there are few things to keep in mind:
#
# I highly recommend reading - http://mywiki.wooledge.org/BashGuide and http://mywiki.wooledge.org/BashFAQ
# Do NOT parse ls output - http://mywiki.wooledge.org/ParsingLs alternatively use find -print0 and xargs -0
# Use bash internals for string manipulation - http://tldp.org/LDP/abs/html/string-manipulation.html
# Good style guide to follow - https://google-styleguide.googlecode.com/svn/trunk/shell.xml
# Use [[ insetad of [ (external binary) - http://mywiki.wooledge.org/BashFAQ/031
# Advanced error handling - http://fvue.nl/wiki/Bash:_Error_handling
# Use getopts instead of getopt - http://mywiki.wooledge.org/BashFAQ/035
#
# Any resource cleanup should happen in a cleanup() function
#
set -euo pipefail
# http://mywiki.wooledge.org/BashFAQ/004
shopt -s nullglob dotglob
PROGNAME=$(basename $0)
# Provide an option to override values via env variables
: ${VAR:="default_value"}
: ${LOCK_FD:="200"}
: ${LOCK_FILE:="/var/lock/${PROGNAME}.lock"}
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')] ($PROGNAME): ERROR: $@" >&2
}
status() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')] ($PROGNAME): $@"
}
lock() {
eval "exec $LOCK_FD>$LOCK_FILE"
flock -n $LOCK_FD || ( err "Cannot aquire lock on ${LOCK_FILE}" ; exit 1; )
}
cleanup() {
shopt -u nullglob dotglob
}
finish() {
local exit_status="${1:-$?}"
if [[ "$exit_status" -eq 0 ]]; then
status "DONE (exit code: ${exit_status})"
else
err "exit code: ${exit_status}"
fi
cleanup
exit $exit_status
}
usage() {
cat <<EOL
USAGE
Descriptive text saying what is the desired function of this program
Usage: $0 [ -b build ] [ -c cleanup ]
EOL
exit 1
}
trap finish EXIT HUP TERM INT SIGHUP SIGINT SIGQUIT SIGTERM ERR
if [ "$#" -lt 1 ]; then
usage
fi
while getopts ":bc" opt; do
case "$opt" in
b)
lock
status '-b invoked'
shift
;;
c)
lock
status '-c option invoked'
shift
;;
*)
usage
;;
esac
done
finish