1+ # #############################################################
2+ # #############################################################
3+ #
4+ # A SERIES OF HELPER FUNCTIONS TO HELP OUT IN
5+ # HANDLING SCRIPTS THAT ARE GROWING IN COMPLEXITY
6+ #
7+ # #############################################################
8+ # #############################################################
9+
10+ quiet_mode () {
11+ # verify quiet mode
12+ # returns 0 if quiet mode is enabled
13+ # returns 1 otherwise
14+ if [ -f /home/pi/quiet_mode ]
15+ then
16+ return 0
17+ else
18+ return 1
19+ fi
20+ }
21+
22+ set_quiet_mode (){
23+ touch /home/pi/quiet_mode
24+ }
25+
26+ unset_quiet_mode (){
27+ delete_file /home/pi/quiet_mode
28+ }
29+
30+
31+ feedback () {
32+ # first parameter is text to be displayed
33+ # this sets the text color to a yellow color for visibility
34+ # the last tput resets colors to default
35+ # one could also set background color with setb instead of setaf
36+ # http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html
37+ echo -e " $( tput setaf 3) $1 $( tput sgr0) "
38+ }
39+
40+ # ########################################################################
41+ #
42+ # FILE EDITION
43+ #
44+ # ########################################################################
45+ delete_line_from_file () {
46+ # first parameter is the string to be matched
47+ # the lines that contain that string will get deleted
48+ # second parameter is the filename
49+ if [ -f $2 ]
50+ then
51+ sudo sed -i " /$1 /d" $2
52+ feedback " deleted $1 from $2 "
53+ fi
54+ }
55+
56+ insert_before_line_in_file () {
57+ # first argument is the line that needs to be inserted DO NOT USE PATHS WITH / in them
58+ # second argument is a partial match of the line we need to find to insert before
59+ # third arument is filename
60+
61+ feedback " Inserting $1 before $2 in $3 "
62+ if [ -f $3 ]
63+ then
64+ feedback " sudo sed -i '/$2 /i $1 ' $3 "
65+ sudo sed -i " /$2 /i $1 " $3
66+ fi
67+ }
68+ add_line_to_end_of_file () {
69+ # first parameter is what to add
70+ # second parameter is filename
71+ if [ -f $2 ]
72+ then
73+ echo $1 >> $2
74+ fi
75+ }
76+
77+ find_in_file () {
78+ # first argument is what to look for
79+ # second argument is the filename
80+ feedback " looking for $1 in $2 "
81+ if grep -q " $1 " $2
82+ then
83+ return 0
84+ else
85+ return 1
86+ fi
87+ }
88+
89+ # ########################################################################
90+ #
91+ # FILE HANDLING - detection, deletion
92+ #
93+ # ########################################################################
94+ file_exists () {
95+ # Only one argument: the file to look for
96+ # returns 0 on SUCCESS
97+ # returns 1 on FAIL
98+ if [ -f $1 ]
99+ then
100+ return 0
101+ else
102+ return 1
103+ fi
104+ }
105+
106+ file_exists_in_folder (){
107+ # can only be run using bash, not sh
108+ # first argument: file to look for
109+ # second argument: folder path
110+ pushd $2
111+ status = file_exists
112+ popd
113+ return status
114+ }
115+
116+ file_does_not_exists (){
117+ # Only one argument: the file to look for
118+ # returns 0 on SUCCESS
119+ # returns 1 on FAIL
120+ feedback " looking for $1 "
121+ if [ ! -f $1 ]
122+ then
123+ feedback " not found $1 "
124+ return 0
125+ else
126+ feedback " found $1 "
127+ return 1
128+ fi
129+ }
130+
131+ delete_file (){
132+ # One parameter only: the file to delete
133+ if file_exists $1
134+ then
135+ sudo rm $1
136+ fi
137+ }
138+
139+ wget_file () {
140+ # One parameter: the URL of the file to wget
141+ # this will look if ther's already a file of the same name
142+ # if there's one, it will delete it before wgetting the new one
143+ # this is to avoid creating multiple files with .1, .2, .3 extensions
144+ echo $1
145+ # extract the filename from the provided path
146+ target_file=${1##*/ }
147+ echo $target_file
148+ delete_file $target_file
149+ wget $1 --no-check-certificate
150+
151+
152+ }
153+
154+ # ########################################################################
155+ #
156+ # FOLDER HANDLING - detection, deletion
157+ #
158+ # ########################################################################
159+ create_folder (){
160+ if ! folder_exists
161+ then
162+ mkdir $1
163+ fi
164+ }
165+
166+ folder_exists (){
167+ # Only one argument: the folder to look for
168+ # returns 0 on SUCCESS
169+ # returns 1 on FAIL
170+ if [ -d $1 ]
171+ then
172+ return 0
173+ else
174+ return 1
175+ fi
176+ }
177+
178+ delete_folder (){
179+ if folder_exists $1
180+ then
181+ sudo rm -r $1
182+ fi
183+ }
0 commit comments