Skip to content

Commit a6a1252

Browse files
author
Dexter Industries
authored
Merge pull request #1 from CleoQc/master
First set of scripts
2 parents 161c2b3 + 27ebf27 commit a6a1252

3 files changed

Lines changed: 285 additions & 0 deletions

File tree

functions_library.sh

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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+
}

install_script_tools.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#! /bin/bash
2+
#####################################################################
3+
#####################################################################
4+
#
5+
# curl
6+
#
7+
#####################################################################
8+
#####################################################################
9+
10+
PIHOME=/home/pi
11+
DEXTER=Dexter
12+
LIB=lib
13+
SCRIPT=script_tools
14+
15+
pushd $PIHOME
16+
result=${PWD##*/}
17+
# check if ~/Dexter exists, if not create it
18+
if [ ! -d $DEXTER ] ; then
19+
echo "creating $PIHOME/$DEXTER"
20+
mkdir $DEXTER
21+
fi
22+
# go into $DEXTER
23+
cd $DEXTER
24+
echo $PWD
25+
26+
# check if /home/pi/Dexter/lib exists, if not create it
27+
if [ ! -d $LIB ] ; then
28+
echo "creating $PIHOME/$DEXTER/$LIB"
29+
mkdir $LIB
30+
fi
31+
cd $LIB
32+
echo $PWD
33+
34+
# check if /home/pi/Dexter/lib/Dexter exists, if not create it
35+
if [ ! -d $DEXTER ] ; then
36+
echo "creating $PIHOME/$DEXTER/$LIB/$Dexter"
37+
mkdir $DEXTER
38+
fi
39+
cd $DEXTER
40+
echo $PWD
41+
42+
# check if /home/pi/Dexter/lib/script_tools exists
43+
# if yes refresh the folder
44+
# if not, clone the folder
45+
if [ -d $SCRIPT ] ; then
46+
echo "Pulling"
47+
cd $SCRIPT
48+
echo $PWD
49+
echo "now in $PIHOME/$DEXTER/$LIB/$SCRIPT"
50+
sudo git pull
51+
else
52+
# clone the folder
53+
echo "Cloning"
54+
sudo git clone https://github.com/DexterInd/script_tools.git
55+
fi
56+
57+
popd

update_wiringpi.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
source /home/pi/di_update/Raspbian_For_Robots/upd_script/functions_library.sh
3+
4+
# Check if WiringPi Installed and has the latest version. If it does, skip the step.
5+
# Gets the version of wiringPi installed
6+
version=`gpio -v`
7+
8+
# Parses the version to get the number
9+
set -- $version
10+
11+
# Gets the third word parsed out of the first line of gpio -v returned.
12+
# Should be 2.36
13+
WIRINGVERSIONDEC=$3
14+
15+
# Store to temp file
16+
echo $WIRINGVERSIONDEC >> tmpversion
17+
18+
# Remove decimals
19+
VERSION=$(sed 's/\.//g' tmpversion)
20+
21+
# Remove the temp file
22+
delete_file tmpversion
23+
24+
feedback "wiringPi VERSION is $VERSION"
25+
if [ $VERSION -eq '236' ]; then
26+
27+
feedback "FOUND WiringPi Version 2.36 No installation needed."
28+
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"
44+
fi
45+
# End check if WiringPi installed

0 commit comments

Comments
 (0)