This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.sh
More file actions
132 lines (109 loc) · 2.54 KB
/
functions.sh
File metadata and controls
132 lines (109 loc) · 2.54 KB
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
#!/bin/bash
: ${SYSLOG_TAG:=}
: ${SYSLOG_FACILITY:=local0}
: ${DEBUG:=0}
: ${LOG_DEBUG:=0}
: ${SETTINGS_FILE:=/app/settings.env}
if [ -n "$SYSLOG_TAG" ]; then
log_pipe=/tmp/bash_log_pipe_$(date +%s)_${RANDOM}.tmp
trap "rm -f $log_pipe" EXIT
mknod $log_pipe p
while read a_line_; do _log info "$a_line_"; done <$log_pipe &
exec 1>&-
exec 1>$log_pipe
exec 2>&1
fi
_log() {
local level=$1; shift;
if [ $# -gt 0 ]; then
if [ -n "$SYSLOG_TAG" ]; then
logger -t "${SYSLOG_TAG}" -p "${SYSLOG_FACILITY}.${level}" -- "$@";
else
echo -E "$(date "+%Y-%m-%dT%H:%M:%S") [$level] $@"
fi
fi
}
bail() {
error "$@. Exiting...";
exit 1;
}
error() {
_log error "$@";
}
info() {
_log info "$@";
}
debug() {
[ "0$LOG_DEBUG" -eq 1 ] &>/dev/null && _log debug "$@" ||:
}
set_debug() {
[ "0$DEBUG" -eq 1 ] &>/dev/null && set -x ||:
}
check_command() {
if [ -n "$1" ] && ! which "$1" &>/dev/null; then
echo "Command '$1' not found"
exit 1;
fi
return 0;
}
file_exists() {
[ -n "$1" -a -f "$1" ]
}
file_not_exists() {
[ -n "$1" -a ! -f "$1" ]
}
bail_file_exists() {
file_not_exists "$1" || bail "$1 already exists; not overwriting"
}
bail_file_not_exists() {
file_exists "$1" || bail "$1 could not be found"
}
fn_exists() {
[[ $(type -t "$1") == function ]]
}
# check if the given value is not empty and is a number
is_number() {
[ "$1" -eq "$1" ] &>/dev/null
}
load_settings_file() {
if [ -f "${SETTINGS_FILE}" ]; then
set -a
source "${SETTINGS_FILE}"
set +a
fi
}
# Test all specified bash tests
#
# E.g: test_all dxw /tmp
# Is the same as writing [ -d /tmp -a -x /tmp -a -w /tmp ]
test_all() {
[ -n "$1" -a -n "$2" ] || bail "Invalid invocation of 'test_all'";
# explode each test
local tests=( $(echo "$1" | fold -w1) )
# stitch together the command array
local cmd=( "-${tests[@]:0:1}" "$2" )
for ((i=1; i <= ${#tests}; i++)); do
cmd+=( -a "-${tests[$i]}" "$2" )
done
# evaluate the command
[ "${cmd[@]}" ]
}
# in_array - check if an element can be found in an array
#
# This will work in most cases though as the "haystack" is passed in as arguments
#
# Usage:
# haystack=(1 2 3 4 5 6)
# needle=4
# if in_array $needle ${haystack[@]}; then
# echo 'found it!';
# fi
in_array() {
local needle="${1}"; shift
while [ $# -gt 0 ]; do
[[ "${1}" == "${needle}" ]] && return 0
shift;
done
return 1
}
check_command logger