-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigTools.sh
219 lines (189 loc) · 4.6 KB
/
ConfigTools.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
#!/bin/bash
#ConfigTools.sh
#This script contains methods which are useful in copying configs to make another analysis
function listConfigs {
if [[ $# < 2 ]]
then
echo "listConfigs <Directory> <Filename>"
return 1
fi
find $1 -name "$2"
}
function findTopOfFirstConfig {
# Takes the path to a config file and returns the line number of the
# first blank line which is above the first config seperated by only
# white space lines.
#Check config exists
if [[ ! (-s $1) ]]
then
echo "File $1 is empty"
DEFAULT_LOCATION="-1"
return
fi
#Get line number for first config
i=`grep -n "Name=" $1 | head -n1 | cut -d: -f1`
#Check we got a config location
if [[ -z $i ]]
then
echo "File $i has no configs"
DEFAULT_LOCATION="-1"
return
fi
line=
while [[ -z $line && $i>1 ]]
do
i=$((i-1));
line=`awk "NR == $i" $1` #Read previous line
#echo $i" : "$line;
if [[ ${line:0:1} == '#' ]]
then
line= #Erase line for comment
else
line=${line// } #kill white space otherwise
fi
done
DEFAULT_LOCATION=$((i+1))
}
function addDefaultToConfigs {
if [[ $# < 3 ]]
then
echo "addDefaultToConfigs <opt=arg> <Directory> <Filename>"
return 1
fi
echo "Would you like to add: DEFAULT::$1"
echo "To the first line of the following:"
find $2 -name "$3"
echo -n "Please type yes if so: "
read input_from_user
if [[ $input_from_user == "yes" ]]
then
for f in `find $2 -name "$3"`
do
findTopOfFirstConfig $f
sed -i.bak $DEFAULT_LOCATION'i\
DEFAULT::'$1'
' $f
#echo "File Succesfully Altered: "`head -n1 $f`
done
echo "File Succesfully Altered"
else
echo "Aborting, nothing added to files..."
fi
}
function replaceInConfigs {
if [[ $# < 4 ]]
then
echo "replaceInConfigs <lineInFile> <Replacement> <Directory> <Filename>"
return 1
fi
echo "Would you like to replace the instances of $1 with $2"
echo "In the following files:"
find $3 -name "$4"
echo -n "Please type yes if so: "
read input_from_user
if [[ $input_from_user == "yes" ]]
then
for f in `find $3 -name "$4"`
do
sed -i.bak "s,$1,$2,g" $f
#echo "File Succesfully Altered: "`head -n1 $f`
done
echo "File Succesfully Altered"
else
echo "Aborting, nothing added to files..."
fi
}
function findConfLocation {
#1 = file location
#2 = Config name
ConfigTools_Conf_Location=`grep -n -m1 "^Name=$2[[:space:]]*$" $1 | cut -d':' -f1`
}
function AddOpt {
#1 == file location
#2 == config name
#3 == opt name
#4 == opt arg
echo "Would you like to add the option $3=$4"
echo "In the config $2 of $1"
echo -n "Please type yes if so: "
read input_from_user
if [[ $input_from_user == "yes" ]]
then
findConfLocation $1 $2
if [[ ! -z $ConfigTools_Conf_Location ]]
then
sed -i.bak "$((ConfigTools_Conf_Location+1)) i$3=$4" $1
echo "File Succesfully Altered"
else
echo "Error: Could not find config named $2 in $1."
fi
else
echo "Aborting, nothing added to files..."
fi
}
function _AddOpt {
#This should not be used by a human, only for automating the addition of optionds
#1 == file location
#2 == config name
#3 == opt name
#4 == opt arg
findConfLocation $1 $2
if [[ ! -z $ConfigTools_Conf_Location ]]
then
sed -i.bak "$((ConfigTools_Conf_Location+1)) i$3=$4" $1
echo "File Succesfully Altered: $1"
else
echo "Error: Could not find config named $2 in $1."
fi
}
function AddOptToDir {
#1 = dir
#2 = file name
#3 = config name
#4 = opt name
#5 = opt arg
echo "Would you like to add the option $4=$5"
echo "To the config $3 in the following files: "
find $1 -name "$2"
echo -n "Please type yes if so: "
read input_from_user
if [[ $input_from_user == "yes" ]]
then
for f in `find $1 -name "$2"`
do
_AddOpt $f $3 $4 $5
done
else
echo "Aborting, nothing added to files..."
fi
}
function mirrorPlots {
if [[ $# < 2 ]]
then
echo "mirrorPlots <Plots From Directory> <Top Level Directory>"
return 1
fi
mirrorPlots_fromdir=$1
mirrorPlots_todirs=$2
echo "Do you want to copy: "
ls ${mirrorPlots_fromdir}/*plots.conf
echo
echo "to: "
echo
find ${mirrorPlots_todirs}/* -type d
echo -n "(yes/no): "
read input_from_user
if [[ $input_from_user == "yes" ]]
then
for i in `ls ${mirrorPlots_fromdir}/*plots.conf`
do
for j in `find ${mirrorPlots_todirs}/* -type d`
do
cp ${i} $j
done
done
echo "File Succesfully Altered"
else
echo "Aborting, nothing added to files..."
fi
}