-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathshell.io
More file actions
executable file
·260 lines (237 loc) · 5.59 KB
/
Copy pathshell.io
File metadata and controls
executable file
·260 lines (237 loc) · 5.59 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/sh
:<<DOCUMENTATION
Description: Defines functions commonly used for file input/output.
Author: Chris Wells (https://chriswells.io)
DOCUMENTATION
################################################################################
# Utility Functions
################################################################################
# Parameters:
# $1 (optional) = number of hex characters to return
# Default is 8 when not provided.
# Returns:
# random hex string of the requested length
# shellcheck disable=SC2120
generateHexString() {
if [ $# -eq 0 ]; then
LENGTH=4
else
LENGTH=$(($1 / 2))
fi
hexdump -n $LENGTH -v -e '/1 "%02X"' /dev/urandom
}
# Parameters:
# $1 (optional) = file for which a temp file needs to be created
# Returns:
# string that can be used as the name of a temp file
# shellcheck disable=SC2119
getTempFileName() {
echo "$1.$(generateHexString).tmp"
}
# Parameters:
# $1 = the name of the jail to run the script inside
# $2 = the code to execute inside the jail
runAsScriptInJail() {
TEMP_SCRIPT="$(getTempFileName 'temp').sh"
createOrReplaceFile "/usr/jails/$1/tmp/$TEMP_SCRIPT" "#!/bin/sh
$2
exit 0
"
chmod +x "/usr/jails/$1/tmp/$TEMP_SCRIPT"
jexec "$1" "/tmp/$TEMP_SCRIPT"
# Delete the script since it's no longer needed. Done here instead of
# inside the script because the script could exit early. -- cwells
rm "/usr/jails/$1/tmp/$TEMP_SCRIPT"
}
################################################################################
# Text Manipulation
################################################################################
# Parameters:
# $1 = text containing special characters
# Returns:
# input string with special characters escaped
escapeAwkSearchText() {
echo "$1" | sed -e 's/[()]/\\\\&/g'
}
# Parameters:
# $1 = sed expression
# Returns:
# input string with special characters escaped
escapeForSed() {
echo "$1" | sed -e 's/[\/&]/\\&/g'
}
# Parameters:
# $1 = text containing newline characters
# Returns:
# input string with newlines escaped
escapeNewlines() {
echo "$1" | awk 1 ORS='\\n'
}
################################################################################
# File Manipulation
################################################################################
# Parameters:
# $1 = file path
# $2 = text to append to file
appendToFile() {
echo "$2" >> "$1"
}
# Parameters:
# $1 = file path
# $2 = text to save as file
createOrReplaceFile() {
echo "$2" > "$1"
}
# Parameters:
# $1 = file path
# $2 = line to delete
deleteLine() {
TEMP_FILE=$(getTempFileName "$1")
if awk -v line="$2" '$0 != line' "$1" > "$TEMP_FILE"; then
cat "$TEMP_FILE" > "$1"
fi
if [ -f "$TEMP_FILE" ]; then
rm "$TEMP_FILE"
fi
}
# Parameters:
# $1 = file path
# $2 = line to insert after
# $3 = text to insert into file
insertAfterLine() {
TEMP_FILE=$(getTempFileName "$1")
if awk -v line="$2" -v insert="$(escapeNewlines "$3")" '
BEGIN {
gsub(/\n$/, "", insert);
}
$0 == line {
$0 = sprintf("%s\n%s", $0, insert);
}
1
' "$1" > "$TEMP_FILE"; then
cat "$TEMP_FILE" > "$1"
fi
if [ -f "$TEMP_FILE" ]; then
rm "$TEMP_FILE"
fi
}
# Parameters:
# $1 = file path
# $2 = line to insert before
# $3 = text to insert into file
insertBeforeLine() {
TEMP_FILE=$(getTempFileName "$1")
if awk -v line="$2" -v insert="$(escapeNewlines "$3")" '
BEGIN {
gsub(/\n$/, "", insert);
}
$0 == line {
$0 = sprintf("%s\n%s", insert, $0);
}
1
' "$1" > "$TEMP_FILE"; then
cat "$TEMP_FILE" > "$1"
fi
if [ -f "$TEMP_FILE" ]; then
rm "$TEMP_FILE"
fi
}
# Parameters:
# $1 = file path
# $2 = old line
# $3 = new line
replaceLine() {
TEMP_FILE=$(getTempFileName "$1")
if awk -v line="$2" -v replace="$(escapeNewlines "$3")" '
BEGIN {
gsub(/\n$/, "", replace);
}
$0 == line {
$0 = replace;
}
1
' "$1" > "$TEMP_FILE"; then
cat "$TEMP_FILE" > "$1"
fi
if [ -f "$TEMP_FILE" ]; then
rm "$TEMP_FILE"
fi
}
# Parameters:
# $1 = file path
# $2 = pattern to match
# $3 = replacement text
replacePattern() {
TEMP_FILE=$(getTempFileName "$1")
if awk -v search="$2" -v replace="$(escapeNewlines "$3")" '
BEGIN {
gsub(/\n$/, "", replace);
}
1 {
gsub(search, replace);
print $0;
}
' "$1" > "$TEMP_FILE"; then
cat "$TEMP_FILE" > "$1"
fi
if [ -f "$TEMP_FILE" ]; then
rm "$TEMP_FILE"
fi
}
# Parameters:
# $1 = file path
# $2 = old text
# $3 = new text
replaceText() {
TEMP_FILE=$(getTempFileName "$1")
if awk -v search="$(escapeAwkSearchText "$2")" -v replace="$(escapeNewlines "$3")" '
BEGIN {
gsub(/\n$/, "", replace);
}
1 {
gsub(search, replace);
print $0;
}
' "$1" > "$TEMP_FILE"; then
cat "$TEMP_FILE" > "$1"
fi
if [ -f "$TEMP_FILE" ]; then
rm "$TEMP_FILE"
fi
}
# Parameters:
# $1 = file path
# $2 = line to comment
# $3 (optional) = string used to comment lines
commentLine() {
if echo "$2" | grep -Eq '^\s*<.+>$'; then # This line is markup. -- cwells
COMMENT=$(echo "$2" | sed -e 's/^\( *\)<\(.*\)>$/\1<!-- \2 -->/')
replaceText "$1" "$2" "$COMMENT"
else
if [ -n "$3" ]; then
COMMENT_MARKER="$3"
else
COMMENT_MARKER="#"
fi
replaceLine "$1" "$2" "$COMMENT_MARKER$2"
fi
}
# Parameters:
# $1 = file path
# $2 = line to uncomment
# $3 (optional) = string used to comment lines
uncommentLine() {
if echo "$2" | grep -Eq '^\s*<.+>$'; then # This line is markup. -- cwells
MARKUP=$(echo "$2" | sed -e 's/^\( *\)<!-- *\(.*\) -->$/\1<\2>/')
replaceText "$1" "$2" "$MARKUP"
else
if [ -n "$3" ]; then
COMMENT_MARKER="$3"
else
COMMENT_MARKER="#"
fi
SEARCH="$(echo "$2" | sed -e 's/[().*^$?&[]/\\\\&/g')"
REPLACE="$(echo "$2" | sed -e 's/&/\\\\&/g')"
replacePattern "$1" "^$COMMENT_MARKER+ *$SEARCH$" "$REPLACE"
fi
}