-
Notifications
You must be signed in to change notification settings - Fork 2
/
_script-templ.sh
executable file
·105 lines (90 loc) · 2.36 KB
/
_script-templ.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
# <SCRIPTNAME>
# <WHAT DOES THIS SCRIPT DO?>
# Copyright (C) 2014-2015 Alexander Swen <alex@swen.nu>
# 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 2 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/>.
# Alexander Swen
# Private contact: alex@swen.nu
# CHANGELOG:
# <DATE> A.Swen created.
# SETTINGS
date=$(date +%Y%m%d)
me=$(basename $0)
mydir=$(dirname $0)
# FUNCTIONS
die () {
rc=$1
shift
printf '%s\n' "=====================" >&2
printf '%s\n' "==== FATAL ERROR ====" >&2
printf '%s\n\n' "=====================" >&2
printf '%s\n\n' "$@" >&2
exit $rc
}
usage () {
printf '%s\n' "===============" >&2
printf '%s\n' "==== USAGE ====" >&2
printf '%s\n\n' "===============" >&2
printf '%s\n' "Usage: ${me} " >&2
printf '%s\n\n' "example: ${me} " >&2
exit 1
}
get_options () {
while [[ $# -gt 0 ]]; do
case "$1" in
--thing1|-a)
shift
declare -r thing="$1"
shift
;;
--thing2|-b)
shift
declare -r other_thing="$1"
shift
;;
-h|--help)
usage
;;
*)
usage
;;
esac
done
}
duration () {
local before=$1
local after="$(date +%s)"
local elapsed="$(expr $after - $before)"
local hours=$(($elapsed / 3600))
local elapsed=$(($elapsed - $hours * 3600))
local minutes=$(($elapsed / 60))
local seconds=$(($elapsed - $minutes * 60))
time_running="${hours}:${minutes}:${seconds}"
}
log () { printf '%s %s\n' "$(date +%F' '%T)" "$@"; }
log_msg () {
duration ${before_total}
message="$1"
echo "${time_running} $1"|tee -a ${short_log}
}
# SCRIPT
before_total="$(date +%s)"
[ ${UID} -gt 0 ] && die 1 "Only root may do that."
log_msg "$(date) started ${me}"
if [[ $# -gt 0 ]];then
get_options
fi
duration ${before_total}
log_msg "Total time taken: ${time_running}"
log_msg "$(date) finished"
# END