Skip to content

Commit 89e8f5d

Browse files
author
Dexter Industries
authored
Merge pull request #4 from CleoQc/master
replace functions! No more fighting with sed!
2 parents b749e77 + 9fb1b97 commit 89e8f5d

2 files changed

Lines changed: 62 additions & 32 deletions

File tree

functions_library.sh

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ delete_line_from_file() {
4949
if [ -f $2 ]
5050
then
5151
sudo sed -i "/$1/d" $2
52-
feedback "deleted $1 from $2"
5352
fi
5453
}
5554

@@ -58,26 +57,55 @@ insert_before_line_in_file() {
5857
# second argument is a partial match of the line we need to find to insert before
5958
# third arument is filename
6059

61-
feedback "Inserting $1 before $2 in $3"
6260
if [ -f $3 ]
6361
then
64-
feedback "sudo sed -i '/$2/i $1' $3"
6562
sudo sed -i "/$2/i $1" $3
6663
fi
6764
}
65+
6866
add_line_to_end_of_file() {
6967
# first parameter is what to add
7068
# second parameter is filename
7169
if [ -f $2 ]
7270
then
73-
echo $1 >> $2
71+
echo "$1" >> "$2"
7472
fi
7573
}
7674

75+
replace_first_this_with_that_in_file() {
76+
# replaces the first occurence
77+
# first parameter is the string to be replaced
78+
# second parameter is the string which replaces
79+
# third parameter is the filename
80+
if grep -q "$1" $3
81+
then
82+
sudo sed -i '/$1/c\$2' $3
83+
return 0
84+
else
85+
#feedback "Line - $1 not found"
86+
return 1
87+
fi
88+
}
89+
replace_all_this_with_that_in_file(){
90+
# does a global replace
91+
# first argument: what needs to be replaced
92+
# second argument: the new stuff
93+
# third argument: the file in question
94+
# returns 0 if file exists (may or may not have succeeded in the substitution)
95+
# return 1 if file does not exists
96+
#feedback "replacing $1 with $2 in $3"
97+
if file_exists "$3"
98+
then
99+
sudo sed -i "s/$1/$2/g" "$3"
100+
return 0
101+
else
102+
return 1
103+
fi
104+
}
105+
77106
find_in_file() {
78107
# first argument is what to look for
79108
# second argument is the filename
80-
feedback "looking for $1 in $2"
81109
if grep -q "$1" $2
82110
then
83111
return 0
@@ -95,7 +123,7 @@ file_exists() {
95123
# Only one argument: the file to look for
96124
# returns 0 on SUCCESS
97125
# returns 1 on FAIL
98-
if [ -f $1 ]
126+
if [ -f "$1" ]
99127
then
100128
return 0
101129
else
@@ -117,22 +145,19 @@ file_does_not_exists(){
117145
# Only one argument: the file to look for
118146
# returns 0 on SUCCESS
119147
# returns 1 on FAIL
120-
feedback "looking for $1"
121148
if [ ! -f $1 ]
122149
then
123-
feedback "not found $1"
124150
return 0
125151
else
126-
feedback "found $1"
127152
return 1
128153
fi
129154
}
130155

131156
delete_file (){
132157
# One parameter only: the file to delete
133-
if file_exists $1
158+
if file_exists "$1"
134159
then
135-
sudo rm $1
160+
sudo rm "$1"
136161
fi
137162
}
138163

@@ -157,17 +182,17 @@ wget_file() {
157182
#
158183
#########################################################################
159184
create_folder(){
160-
if ! folder_exists
185+
if ! folder_exists "$1"
161186
then
162-
mkdir $1
187+
sudo mkdir "$1"
163188
fi
164189
}
165190

166191
folder_exists(){
167192
# Only one argument: the folder to look for
168193
# returns 0 on SUCCESS
169194
# returns 1 on FAIL
170-
if [ -d $1 ]
195+
if [ -d "$1" ]
171196
then
172197
return 0
173198
else
@@ -176,8 +201,8 @@ folder_exists(){
176201
}
177202

178203
delete_folder(){
179-
if folder_exists $1
204+
if folder_exists "$1"
180205
then
181-
sudo rm -r $1
206+
sudo rm -r "$1"
182207
fi
183208
}

update_wiringpi.sh

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#!/bin/bash
22
source /home/pi/di_update/Raspbian_For_Robots/upd_script/functions_library.sh
33

4+
PIHOME=/home/pi
5+
DEXTER=Dexter
6+
LIB=lib
7+
8+
49
# Check if WiringPi Installed and has the latest version. If it does, skip the step.
510
# Gets the version of wiringPi installed
611
version=`gpio -v`
@@ -24,22 +29,22 @@ delete_file tmpversion
2429
feedback "wiringPi VERSION is $VERSION"
2530
if [ $VERSION -eq '236' ]; then
2631

27-
feedback "FOUND WiringPi Version 2.36 No installation needed."
32+
feedback "FOUND WiringPi Version 2.36 No installation needed."
2833
else
29-
feedback "Did NOT find WiringPi Version 2.36"
30-
# Check if the Dexter directory exists.
31-
DIRECTORY='/home/pi/Dexter'
32-
create_folder "$DIRECTORY"
33-
create_folder "$DIRECTORY"/lib
34-
35-
# Change directories to Dexter
36-
cd $DIRECTORY/lib
37-
delete_folder wiringPi
38-
# Install wiringPi
39-
git clone https://github.com/DexterInd/wiringPi/ # Clone directories to Dexter.
40-
cd wiringPi
41-
sudo chmod +x ./build
42-
sudo ./build
43-
feedback "wiringPi Installed"
34+
feedback "Did NOT find WiringPi Version 2.36"
35+
# Check if the Dexter directory exists.
36+
create_folder "$PIHOME/$DEXTER"
37+
create_folder "$PIHOME/$DEXTER/$LIB"
38+
39+
# Change directories to Dexter/lib
40+
cd $PIHOME/$DEXTER/$LIB
41+
42+
delete_folder wiringPi
43+
# Install wiringPi
44+
git clone https://github.com/DexterInd/wiringPi/ # Clone directories to Dexter.
45+
cd wiringPi
46+
sudo chmod +x ./build
47+
sudo ./build
48+
feedback "wiringPi Installed"
4449
fi
4550
# End check if WiringPi installed

0 commit comments

Comments
 (0)