-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfunctions_library.sh
executable file
·361 lines (321 loc) · 8.17 KB
/
functions_library.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
##############################################################
##############################################################
#
# A SERIES OF HELPER FUNCTIONS TO HELP OUT IN
# HANDLING SCRIPTS THAT ARE GROWING IN COMPLEXITY
#
##############################################################
##############################################################
quiet_mode() {
# verify quiet mode
# returns 0 if quiet mode is enabled
# returns 1 otherwise
if [ -f /home/pi/quiet_mode ]
then
return 0
else
return 1
fi
}
set_quiet_mode(){
touch /home/pi/quiet_mode
}
unset_quiet_mode(){
delete_file /home/pi/quiet_mode
}
feedback() {
# first parameter is text to be displayed
# this sets the text color to a yellow color for visibility
# the last tput resets colors to default
# one could also set background color with setb instead of setaf
#http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html
echo -e "$(tput setaf 3)$1$(tput sgr0)"
}
# Function checks out branch if BRANCH is defined.
change_branch() {
# first and only parameter is the branch to checkout
# -z tests for zero length.
if [ -z ${1+x} ]; then
echo "Working from main branch.";
else
echo "Working from $1 branch";
# sudo git checkout -b $BRANCH
# the -b creates a branch if it doesn't exist
# this leads to a fatal error msg being displayed to the user
# is there any case where we can to create the branch here???
# https://github.com/tldr-pages/tldr/blob/master/pages/common/git-checkout.md
sudo git checkout $1
fi
}
###########################################################################
#
# USB drive stuff - DEXTEROS ONLY
#
###########################################################################
# get_usb_mount_point - get the USB drive directory (the mount point), if present
#
# writes the path name to stdout
#
# Returns: 0 on success or 1 on failure
get_usb_mount_point() {
# devmon mounts usb drive to /media/
active_drives=$(ls /dev/disk/by-partuuid/ | tr '\n' ' ' 2>/dev/null)
real_media_points=""
for active_drive in $active_drives
do
output=$(findmnt -rn -S PARTUUID="$active_drive" -o TARGET)
errcode=$?
if [[ $errcode == 0 ]]; then
real_media_points="$real_media_points$output "
fi
done
real_media_points=$(echo -e "$real_media_points")
OIFS="$IFS"
IFS=$'\n'
find /media -maxdepth 1 -mindepth 1 | while read media_point
do
# spaces in the compared strings must stay
if [[ "$real_media_points " = *"$media_point "* ]]; then
echo "$media_point"
return 0
fi
done
IFS="$OIFS"
return 1
}
# get_usb_symlink_for_user - get the USB drive directory (the symlink to the user's home directory), if present
#
# writes the path name to stdout
#
# Returns: 0 on success or 1 on failure
get_usb_symlink_for_user() {
if [[ -L $HOME/USB-Drive ]]; then
echo "$HOME/USB-Drive"
return 0
fi
return 1
}
#########################################################################
#
# SCRIPT HELPERS
#
#########################################################################
where_am_i() {
# return the directory where the script resides,
# path is relative to where it was called from
# includes the filename
# call like this: here=$(where_am_i)
echo $(dirname $(readlink -f $0))
}
where_am_i_fullpath() {
# returns the full path to the folder containing the current script
# does not contain the script name
echo $( cd "$(dirname "$BASH_SOURCE")" ; pwd -P )
}
who_called_me() {
# returns the calling script if any
# path is relative
# otherwise returns empty
echo ${BASH_SOURCE[1]}
}
check_internet() {
# check if there's internet access
# and if there's not, exit the script
if ! quiet_mode ; then
feedback "Check for internet connectivity..."
feedback "=================================="
wget -q --tries=2 --timeout=20 --output-document=/dev/null https://raspberrypi.org
if [ $? -eq 0 ];then
echo "Connected to the Internet"
else
echo "Unable to Connect, try again !!!"
exit 0
fi
fi
}
#########################################################################
#
# FILE EDITION
#
#########################################################################
delete_line_from_file() {
# first parameter is the string to be matched
# the lines that contain that string will get deleted
# second parameter is the filename
if [ -f $2 ]
then
sudo sed -i "/$1/d" $2
fi
}
insert_before_line_in_file() {
# first argument is the line that needs to be inserted DO NOT USE PATHS WITH / in them
# second argument is a partial match of the line we need to find to insert before
# third arument is filename
if [ -f $3 ]
then
sudo sed -i "/$2/i $1" $3
fi
}
add_line_to_end_of_file() {
# first parameter is what to add
# second parameter is filename
if [ -f $2 ]
then
echo "$1" >> "$2"
fi
}
sudo_add_line_to_end_of_file() {
if [ -f $2 ]
then
if ! grep "$1" "$2"
then
sudo bash -c "echo $1 >> $2"
fi
fi
}
replace_first_this_with_that_in_file() {
# replaces the first occurence
# first parameter is the string to be replaced
# second parameter is the string which replaces
# third parameter is the filename
if grep -q "$1" $3
then
sudo sed -i "s/$1/$2/" "$3"
return 0
else
#feedback "Line - $1 not found"
return 1
fi
}
replace_all_this_with_that_in_file(){
# does a global replace
# first argument: what needs to be replaced
# second argument: the new stuff
# third argument: the file in question
# returns 0 if file exists (may or may not have succeeded in the substitution)
# return 1 if file does not exists
#feedback "replacing $1 with $2 in $3"
if file_exists "$3"
then
sudo sed -i "s/$1/$2/g" "$3"
return 0
else
return 1
fi
}
find_in_file() {
# first argument is what to look for
# second argument is the filename
if grep -q "$1" $2
then
return 0
else
return 1
fi
}
find_in_file_strict() {
# first argument is what to look for
# second argument is the filename
# looks for a complete word and not part of a word
if grep -w -q "$1[\D]" $2
then
return 0
else
return 1
fi
}
#########################################################################
#
# FILE HANDLING - detection, deletion
#
#########################################################################
file_exists() {
# Only one argument: the file to look for
# returns 0 on SUCCESS
# returns 1 on FAIL
if [ -f "$1" ]
then
return 0
else
return 1
fi
}
file_exists_in_folder(){
# can only be run using bash, not sh
# first argument: file to look for
# second argument: folder path
pushd $2 > /dev/null
status = file_exists $1
popd > /dev/null
return status
}
file_does_not_exists(){
# Only one argument: the file to look for
# returns 0 on SUCCESS
# returns 1 on FAIL
if [ ! -f $1 ]
then
return 0
else
return 1
fi
}
delete_file (){
# One parameter only: the file to delete
if file_exists "$1"
then
sudo rm "$1"
fi
}
wget_file() {
# One parameter: the URL of the file to wget
# this will look if ther's already a file of the same name
# if there's one, it will delete it before wgetting the new one
# this is to avoid creating multiple files with .1, .2, .3 extensions
echo $1
# extract the filename from the provided path
target_file=${1##*/}
echo $target_file
delete_file $target_file
wget $1 --no-check-certificate
}
#########################################################################
#
# FOLDER HANDLING - detection, deletion
#
#########################################################################
create_folder(){
if ! folder_exists "$1"
then
sudo mkdir -p "$1"
fi
}
create_folder_nosudo(){
if ! folder_exists "$1"
then
mkdir -p "$1"
fi
}
create_folder_nosudo(){
if ! folder_exists "$1"
then
mkdir -p "$1"
fi
}
folder_exists(){
# Only one argument: the folder to look for
# returns 0 on SUCCESS
# returns 1 on FAIL
if [ -d "$1" ]
then
return 0
else
return 1
fi
}
delete_folder(){
if folder_exists "$1"
then
sudo rm -r "$1"
fi
}