-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathIII Control Statements in Bash Scripting.py
309 lines (252 loc) · 7.95 KB
/
III Control Statements in Bash Scripting.py
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
"""
You’ll learn the differences between FOR, WHILE, IF, and CASE statements and how to use them in your Bash scripts.
Armed with these new tools, you’ll be ready to create more advanced Bash scripts with conditional logic.
"""
"""
*** IF statements
*** sytax
>>>>>> if [ condition ]; then
# some code
else
# some other code
fi
# double parethesis structure
eg:
if (($x > 5)); then
echo "$x is more than 5!"
fi
# square brackets and arithmetic flags structure
>>>>>> -eq : 'equal to'
>>>>>> -ne : 'not equal to'
>>>>>> -lt : 'less than'
>>>>>> -le : 'less than or equal to'
eg:
if [ $x -gt 5 ]; then
echo "$x is grater than 5!"
fi
*** bash Conditional Flags
>>>>>> -e : 'file exists'
>>>>>> -s : 'if exists and it's grater than zero'
>>>>>> -r : 'if exists and readable'
>>>>>> -w : 'if exists and writable'
*** AND OR in bash
>>>>>> && : 'and'
>>>>>> || : 'or'
eg:
# Simple-square notation
if [ $x -gt 5 ] && [ $x -lt 11 ]; then
echo "$x more than 5, less than 11"
fi
# Double-square notation
if [[ $x -gt 5 && ]]; then
echo "$x more than 5, less than 11"
fi
"""
"""
### Sorting model results
Instructions
- Create a variable, accuracy by extracting the "Accuracy" line (and "Accuracy" value) in the first ARGV element (a file).
- Create an IF statement to move the file into good_models/ folder if it is greater than or equal to 90 using a flag, not a mathematical sign.
- Create an IF statement to move the file into bad_models/ folder if it is less than 90 using a flag, not a mathematical sign.
- Run your script from the terminal pane twice (using bash script.sh). Once with model_results/model_1.txt, then once with model_results/model_2.txt as the
only argument.
"""
# Extract Accuracy from first ARGV element
accuracy=$(grep Accuracy $1 | sed 's/.* //')
# Conditionally move into good_models folder
if [ $accuracy -ge 90 ]; then
mv $1 good_models/
fi
# Conditionally move into bad_models folder
if [ $accuracy -lt 90 ]; then
mv $1 bad_models/
fi
repl:~/workspace$ bash script.sh model_results/model_1.txt
repl:~/workspace$ bash script.sh model_results/model_2.txt
"""
### Moving relevant files
Instructions
- Create a variable sfile out of the first ARGV element.
- Use an IF statement and grep to check if the sfile variable contains SRVM_ AND vpt inside.
- Inside the IF statement, move matching files to the good_logs/ directory.
- Try your script on all of the files in the directory (that is, run it four times - once for each file). It should move only one of them.
"""
# Create variable from first ARGV element
sfile=$1
# Create an IF statement on sfile's contents
if grep -q 'SRVM_' $sfile && grep -q 'vpt' $sfile; then
# Move file if matched
mv $sfile good_logs/
fi
repl:~/workspace$ bash script.sh logfiles8.txt
repl:~/workspace$ bash script.sh logdays.txt
repl:~/workspace$ bash script.sh log1.txt
"""
*** FOR loops & WHILE
*** syntax
for z in 1 2 3
do
echo $z
done
# num range 'brace expansion'
{START..STOP..INCREMENT}
for x in {1..3..2}
do
echo $x
done
# three expression syntax
# -double parenthesis
# -{START..STOP..INCREMENT}
eg:
for ((x=2;x>=4;x+=2))
do
echo $x
done
-2
-4
# glob expansions
for book in books/*
do
echo $book
done
# shell-w/in-a-shell to FOR loop
for book in $(ls books/ | grep -i 'air')
do
echo $book
done
-AirportBook.txt
-FairMarketBook.txt
*** WHILE
eg:
x=1
while [ $x -le 3 ];
do
echo $x
((x+=1))
done
"""
"""
### A simple FOR loop
Instructions
- Use a FOR statement to loop through files that end in .R in inherited_folder/ using a glob expansion.
- echo out each file name into the console.
"""
# Use a FOR loop on files in directory
for file in inherited_folder/*.R
do
# Echo out each file
echo $file
done
"""
### Correcting a WHILE statement
can you determine where the mistake is? What will happen if it runs?
You can find the code by using cat emp_script.sh to print it to the console.
Instructions
Possible Answers :
@ There is no mistake, this script will run just fine.
@ It will run forever because emp_num isn't incremented inside the loop.
@ You cannot cat a .txt file so this will fail.
Answer : It will run forever because emp_num isn't incremented inside the loop.
"""
"""
### Cleaning up a directory
Instructions
- Use a FOR statement to loop through (using glob expansion) files that end in .py in robs_files/.
- Use an IF statement and grep (remember the 'quiet' flag?) to check if RandomForestClassifier is in the file. Don't use a shell-within-a-shell here.
- Move the Python files that contain RandomForestClassifier into the to_keep/ directory.
"""
# Create a FOR statement on files in directory
for file in robs_files/*.py
do
# Create IF statement using grep
if grep -q 'RandomForestClassifier' $file ; then
# Move wanted files to to_keep/ folder
mv $file to_keep/
fi
done
repl:~/workspace$ cd /home/repl/workspace
repl:~/workspace$ bash script.sh
"""
*** CASE statements
*** basic syntax
>>>>>> case 'something' in
*** regex for pattern
>>>>>> Air* : 'starts w/ Air'
>>>>>> *hat* : 'contains hat'
***syntax
case 'something' in
PATTERN1)
COMMAND1;;
PATTERN2)
COMMAND2;;
*)
DEFAULT COMMAND;;
esac
eg: #new case statement
case $(cat $1) in
*Sydney*)
mv $1 sydney/ ;;
*melbourne*|*brisbane*)
rem $1 ;;
*canberra*)
mv $1 "IMPORTANT_$1" ;;
*)
echo "no cities found!" ;;
esac
"""
"""
### Days of the week with CASE
Instructions
- Build a CASE statement that matches on the first ARGV element.
- Create a match on each weekday such as Monday, Tuesday etc. using OR syntax on a single line, then a match on each weekend day (Saturday and Sunday) etc.
using OR syntax on a single line.
- Create a default match that prints out Not a day! if none of the above patterns are matched.
- Save your script and run in the terminal window with Wednesday and Saturday to test.
"""
# Create a CASE statement matching the first ARGV element
case $1 in
# Match on all weekdays
Monday|Tuesday|Wednesday|Thusday|Friday)
echo "It is a Weekday!" ;;
# Match on all weekend days
Saturday|Sunday)
echo "It is a Weekend!" ;;
# Create a default
*)
echo "Not a day!" ;;
esac
repl:~/workspace$ bash script.sh Wednesday
It is a Weekday!
repl:~/workspace$ bash script.sh Saturday
It is a Weekend!
"""
### Moving model results with CASE
Your task is to use a CASE statement to move the tree-based models (Random Forest, GBM, and XGBoost)
to the tree_models/ folder, and delete all other models (KNN and Logistic).
Instructions
- Use a FOR statement to loop through (using glob expansion) files in model_out/.
- Use a CASE statement to match on the contents of the file (we will use cat and shell-within-a-shell to get the contents to match against). It must check if the
text contains a tree-based model name and move to tree_models/, otherwise delete the file.
- Create a default match that prints out Unknown model in FILE where FILE is the filename then run your script.
"""
# Use a FOR loop for each file in 'model_out/'
for file in model_out/*
do
# Create a CASE statement for each file's contents
case $(cat $file) in
# Match on tree and non-tree models
*"Random Forest"*|*GBM*|*XGBoost*)
mv $file tree_models/ ;;
*KNN*|*Logistic*)
rm $file ;;
# Create a default
*)
echo "Unknown model in $file" ;;
esac
done
"""
### Finishing a CASE statement
-What is the correct way to finish a CASE statement?
Answer the question
Answer : esac
"""