forked from chriswells0/freebsd-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.common
executable file
·180 lines (155 loc) · 4.15 KB
/
shell.common
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
178
179
180
#!/bin/sh
:<<DOCUMENTATION
Description: Defines functions frequently used in shell scripts.
Author: Chris Wells (https://chriswells.io)
DOCUMENTATION
# Returns:
# full path to the current script
getScriptPath() {
dirname "$(readlink -f "$0")"
}
# Parameters:
# $1 = relative or full path to the script to import
importScript() {
SCRIPT_PATH=$(getScriptPath)
if [ -f "$SCRIPT_PATH/lib/$1" ]; then
# shellcheck source=/dev/null
. "$SCRIPT_PATH/lib/$1"
elif [ -f "$SCRIPT_PATH/../lib/$1" ]; then
# shellcheck source=/dev/null
. "$SCRIPT_PATH/../lib/$1"
elif [ -f "$SCRIPT_PATH/$1" ]; then
# shellcheck source=/dev/null
. "$SCRIPT_PATH/$1"
elif [ -f "$1" ]; then
# shellcheck source=/dev/null
. "$1"
else
echo "Error: '$1' was not found."
exit 1
fi
}
# Parameters:
# $1 (optional) = action that occurs after pressing Enter
# Default is 'continue' when not provided.
pressEnterTo() {
if [ -n "$1" ]; then
ACTION="$1"
else
ACTION='continue'
fi
# shellcheck disable=SC2039
read -r -p "
Press \"Enter\" to $ACTION..." _
}
# Parameters:
# $1 (optional) = custom error message to display if the user is not root
requireRootOrExit() {
if [ "$(id -u)" != "0" ]; then
if [ -n "$1" ]; then
MESSAGE="$1"
else
MESSAGE='This script must be executed as root or using sudo.'
fi
echo "$MESSAGE" 1>&2
exit 1
fi
}
################################################################################
# Hashes (Associative Arrays / Dictionaries)
################################################################################
# Parameters:
# $1 = name of the hash variable
# $2 = key to search for
# Returns:
# result code indicating whether the specified key exists in the hash
hashContainsKey() {
alias "HASH_$1_$2" > /dev/null 2>&1
}
# Parameters:
# $1 = name of the hash variable
# $2 = value to search for
# Returns:
# result code indicating whether the specified value exists in the hash
hashContainsValue() {
alias | grep -Eq "^HASH_$1_.+='?$2'?\$"
}
# Parameters:
# $1 = name of the hash variable
# $2 = key for the value that should be retrieved
# Returns:
# value found for the specified key
hashGet() {
alias "HASH_$1_$2" 2> /dev/null | awk -F"=" '{ gsub(/^'\''|'\''$/, "", $2); print $2; }'
}
# Parameters:
# $1 = name of the hash variable
# $2 = key to use
# $3 (optional) = the value to store
# Default is an empty string when not provided.
hashPut() {
# shellcheck disable=SC2139
alias "HASH_$1_$2=$3"
}
################################################################################
# Script Options
################################################################################
# Parameters:
# $1 = name of the option whose value should be returned
# Returns:
# value provided for the specified option
getOption() {
hashGet 'SCRIPT_OPTS' "$1"
}
# Parameters:
# $1 = name of the option to check
# Returns:
# result code indicating whether the specified option was passed to the script
optionIsEnabled() {
hashContainsKey 'SCRIPT_OPTS' "$1"
}
# Parameters:
# $@ = full list of options passed to the script
parseScriptOptions() {
for SCRIPT_ARG in "$@"; do
case $SCRIPT_ARG in
-* )
setOption "$SCRIPT_ARG"
LAST_SWITCH=$SCRIPT_ARG ;;
* )
if [ -n "$LAST_SWITCH" ]; then
setOption "$LAST_SWITCH" "$SCRIPT_ARG"
else
setOption "$SCRIPT_ARG"
fi
LAST_SWITCH='' ;;
esac
done
}
# Parameters:
# $1 = name of the option to set
# $2 = value to set for the specified option
setOption() {
hashPut 'SCRIPT_OPTS' "$1" "$2"
}
################################################################################
# Note Management
################################################################################
# Parameters:
# $1 = message to append to the notes for the currently running script
addNote() {
# Not using appendToFile here to ensure this file has no dependencies. -- cwells
#appendToFile "$0.notes" "$1"
echo "$1" >> "$(getScriptPath)/$(basename $0).notes"
}
# Parameters:
# $1 (optional) = message to display above the notes for this script
showNotes() {
if [ -n "$1" ]; then
MESSAGE="$1"
else
MESSAGE='The following notes were saved in'
fi
echo "$MESSAGE $0.notes:
$(cat "$(getScriptPath)/$(basename $0).notes")"
}