-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.sh
executable file
·55 lines (47 loc) · 1.29 KB
/
common.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
#!/usr/bin/env bash
#
# Common script functions
# Copyright (c) 2020 - SCPG <[email protected]>
#
# Credits:
# Some ideas taken from:
# * https://github.com/ralish/bash-script-template
# * https://github.com/z017/shell-script-skeleton
#######################################
# CONSTANTS & VARIABLES
#######################################
# Verbose Levels
readonly VERBOSE_LEVELS=(none fatal error warning info debug)
# Level Colors
readonly LEVEL_COLORS=(39 31 31 33 32 36)
# Defaults Verbose Level - 0 none, 1 fatal, 2 error, 3 warning, 4 info, 5 debug
readonly VERBOSE_DEFAULT=5
# Current verbose level
declare -i verbose_level="$VERBOSE_DEFAULT"
#######################################
# FUNCTIONS
#######################################
# Print out error messages to STDERR.
function err() {
[[ $verbose_level -ge 1 ]] \
&& echo -e "\033[0;${LEVEL_COLORS[1]}mERROR: $@\033[0m" >&2
}
# Shows an error if required tools are not installed.
function required {
local e=0
for tool in "$@"; do
type $tool >/dev/null 2>&1 || {
e=1 && err "$tool is required for running this script. Please install $tool and try again."
}
done
[[ $e < 1 ]] || exit 2
}
# Version
function version() {
echo "$SCRIPT_NAME version $VERSION"
}
# Help
function help() {
echo "$@" >&2
exit 1
}