-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirgraph_test
executable file
·443 lines (372 loc) · 10.5 KB
/
dirgraph_test
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/bin/bash
#nasteveni konzole
set -u -o pipefail
# -o pipefail navratova hodnoty pipeline je urcena poslednim prikazem
# -u povazuje nenastavene vychozi hodnoty promennych za chybu
# -x umoznuje sledovani prubehu vykonavani prikazu a jejich tisk na konzoli
tabs 4
export POSIXLY_CORRECT=YES
count_directories () { #funkce pro pocitani slozek
echo "Predano $# parametru: $@"
num=`find "$1" -not -path '*/.*' -type d 2>/dev/null | egrep -v "$2" | wc -l`
exit_code=$?
printf "Directories: %d\n" $num
return $exit_code
}
count_files () { #funkce pro pocitani souboru
echo "Predano $# parametru: $@"
num=`find "$1" -not -path '*/.*' -type f 2>/dev/null | egrep -v "$2" | wc -l`
exit_code=$?
printf "All files: %d\n" $num
return $exit_code
}
make_fhist () {
echo "Predano $# parametru: $@"
file_list=`find "$1" -not -path '*/.*' -type f -print 2>/dev/null | egrep -v "$3"`
ret_val=$? #navratova hodnota posledniho prikazu urcuje navratovy kod dane funkce
# -not path '...' ignoruje cesty se skrytymi adresari a soubory
# -type f zameruje se na soubory
# -exec ... {} + provadi nad kazdym vstupem funkci wc(nemuze byt execdir, protoze neuvadi celou cestu vzhledem k aktualnimu adresari
# wc -c vypisuje nad kazdym souborem jeho velikost v bajtech
# v pripade davky souboru vypisuje jeste celkovou velikost
# sed '$1' textovy editor odstranuje posledni radek s napisem TOTAL (neplati na stroji EVA)
# nemusi byt pouzito pri kombinaci find . -not -path '*/.*' -type f -exec wc -c '{}' \;
# gawk '{print $1}' vypisuje pouze prvni sloupec vstupu
typeset -A cn=([lt100]=0 [lt1Ki]=0 [lt10Ki]=0 [lt100Ki]=0 [lt1Mi]=0 [lt10Mi]=0 [lt100Mi]=0 [lt1Gi]=0 [gt1Gi]=0)
#echo $file_list
ret_val2=0
byte_num=0
while IFS=$'\n' read -r row
do
byte_num=`wc -c "$row" | awk '{print $1}'`
ret_val2=$?
if [ $ret_val2 -ne 0 ]
then
continue
fi
echo "Pocet bajtu: $byte_num"
if (( $byte_num < 100 ))
then
cn[lt100]=`expr ${cn[lt100]} + 1`
elif (( $byte_num >= 100 && $byte_num < 1024 ))
then
cn[lt1Ki]=`expr ${cn[lt1Ki]} + 1`
elif (( $byte_num >= 1024 && $byte_num < 10240 ))
then
cn[lt10Ki]=`expr ${cn[lt10Ki]} + 1`
elif (( $byte_num >= 10240 && $byte_num < 102400 ))
then
cn[lt100Ki]=`expr ${cn[lt100Ki]} + 1`
elif (( $byte_num >= 102400 && $byte_num < 1048576 ))
then
cn[lt1Mi]=`expr ${cn[lt1Mi]} + 1`
elif (( $byte_num >= 1048576 && $byte_num < 10485760 ))
then
cn[lt10Mi]=`expr ${cn[lt10Mi]} + 1`
elif (( $byte_num >= 10485760 && $byte_num < 104857600 ))
then
cn[lt100Mi]=`expr ${cn[lt100Mi]} + 1`
elif (( $byte_num >= 104857600 && $byte_num < (1024*1048576) ))
then
cn[lt1Gi]=`expr ${cn[lt1Gi]} + 1`
else
cn[gt1Gi]=`expr ${cn[gt1Gi]} + 1`
fi
done <<< "$file_list"
echo "Parametry funkce $0: $*"
cols=1 #pocet sloupcu
catname_length=11 #delka nazvu kategorie
norm_on="no" #rozhodnuti o normalizaci
if [ "$2" = "-n" ] #podminka, zda chci normalizovat graf
then
echo "Normalizace vyzadana uzivatelem..."
if [ -t 1 ] #pokud skript bezi v terminalu
then
cols=`expr $(tput cols) - 1 - $catname_length`
else #pokud bezi skript nekde jinde
cols=79
fi
echo "Pocet sloupcu: $cols"
max=0
for idx in ${!cn[@]} #naleznu maximum
do
if [ ${cn[$idx]} -gt $max ]; then max=${cn[$idx]}; fi
done
echo "Max: $max"
cf=0
if [ $max -gt $cols ]; then #pokud je max vetsi jak delka radku, zacnu normalizovat
norm_on="yes"
cf=$(echo "scale=5; $cols / $max" | bc)
fi
echo "Koeficinet: $cf"
echo "Normalizace zapnuta: $norm_on"
for idx in "${!cn[@]}" #normalizuji...
do
if [ "$norm_on" = "yes" ]
then
echo "Kategorie $idx hodnota: ${cn[$idx]}"
cn[$idx]=$(echo "scale=5; ${cn[$idx]} * $cf" | bc)
b=${cn[$idx]} #value before rounding
echo "Kategorie $idx hodnota PRED zaokrouhlenim: ${cn[$idx]}"
cn[$idx]=$(printf "%.0f" "${cn[$idx]}")
a=${cn[$idx]} #value after rounding
echo "Kategorie $idx hodnota PO zaokrouhleni: ${cn[$idx]}"
res1=$(echo "if($b!=0) 1" | bc -l)
echo "Podminka 1: \"$res1\""
res2=$(echo "if($a==0) 1" | bc -l)
echo "Podminka 2: \"$res2\""
if [ "$res1" = "1" -a "$res2" = "1" ] #zaokrouhlovani na jednicku
then
echo "Srazim cislo na jednicku..."
cn[$idx]=1
fi
echo "Kategorie $idx konecna hodnota: ${cn[$idx]}"
echo
else
break
fi
done
fi
echo "File size histogram:"
printf " %s%3s " "<100 B" ":"
idx=0
while [ $idx -lt ${cn[lt100]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
echo ${cn[lt100]}
printf " %s%3s " "<1 KiB" ":"
idx=0
while [ $idx -lt ${cn[lt1Ki]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
echo ${cn[lt1Ki]}
printf " %s%2s " "<10 KiB" ":"
idx=0
while [ $idx -lt ${cn[lt10Ki]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
echo ${cn[lt10Ki]}
printf " %s%s " "<100 KiB" ":"
idx=0
while [ $idx -lt ${cn[lt100Ki]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
echo ${cn[lt100Ki]}
printf " %s%3s " "<1 MiB" ":"
idx=0
while [ $idx -lt ${cn[lt1Mi]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
echo ${cn[lt1Mi]}
printf " %s%2s " "<10 MiB" ":"
idx=0
while [ $idx -lt ${cn[lt10Mi]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
echo ${cn[lt10Mi]}
printf " %s%s " "<100 MiB" ":"
idx=0
while [ $idx -lt ${cn[lt100Mi]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
echo ${cn[lt100Mi]}
printf " %s%3s " "<1 GiB" ":"
idx=0
while [ $idx -lt ${cn[lt1Gi]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
echo ${cn[lt1Gi]}
printf " %s%2s " ">=1 GiB" ":"
idx=0
while [ $idx -lt ${cn[gt1Gi]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
echo ${cn[gt1Gi]}
if [ $ret_val1 -ne 0 -o $ret_val2 -ne 0 ]
then
return 1 #navratova hodnota cele funkce
else
return 0
fi
}
if [ $# -eq 0 ] #pripad, kdy nebyl nalezen zadny parametr, skript bere aktualni adresar
then
echo "Nezadan zadny pozicni parametr..."
echo "Root directory: $PWD"
count_directories "$PWD" '!.'
echo "Navratovy kod: $?"
count_files "$PWD" '!.'
echo "Navratovy kod: $?"
make_fhist "$PWD" x '!.'
echo "Navratovy kod: $?"
exit $?
elif [ $# -eq 1 ] #byl zadan jeden pozicni parametr
then
echo "Zadany 1 pozicni parametr"
if [ "$1" = "-n" ] #pozicni parametr je prepinac normalizace grafu
then
echo "Root directory: $PWD"
count_directories "$PWD" '!.'
echo "Navratovy kod: $?"
count_files "$PWD" '!.'
echo "Navratovy kod: $?"
make_fhist "$PWD" $1 '!.'
echo "Navratovy kod: $?"
exit $?
elif [ -d "$1" ] #pozicni parametr je nazev adresare
then
echo "Root directory: $1"
count_directories "$1" '!.'
echo "Navratovy kod: $?"
count_files "$1" '!.'
echo "Navratovy kod: $?"
make_fhist "$1" x '!.'
echo "Navratovy kod: $?"
exit $?
elif [[ "$1" =~ [-*] ]]
then
echo "Invalid option!" >/dev/stderr
exit 64
else
echo "$1:Not a name of a directory!" >/dev/stderr
exit 66
fi
elif [ $# -eq 2 ] #zadany dva pozicni parametry
then
echo "Zadany 2 pozicni parametry..."
if [ "$1" = "-i" ]
then
if [ -n "$2" ] #test, zda je retezec za prepinacem nenulovy
then
if [ "$2" = "/" ] #nesmi pokryvat nazev korenoveho adresare
then
echo "Ignored-files list should not include a name of the root directory!" >/dev/stderr
exit 64
else
echo "Root directory: $PWD"
count_directories "$PWD" "$2"
echo "Navratovy kod: $?"
count_files "$PWD" "$2"
echo "Navratovy kod: $?"
make_fhist "$PWD" x "$2"
echo "Navratovy kod: $?"
exit $?
fi
else
echo "Valid regular expression must follow!" >/dev/stderr
exit 65
fi
elif [ "$1" = "-n" ]
then
if [ -d "$2" ] #testovani, zda druhy pozicni parametr je nazev adresare
then
echo "Root directory: $2"
count_directories "$2" '!.'
echo "Navratovy kod: $?"
count_files "$2" '!.'
echo "Navratovy kod: $?"
make_fhist "$2" $1 '!.'
echo "Navratovy kod: $?"
exit $?
else
echo "$2:Not a name of a directory!" >/dev/stderr
exit 66 #66- neexistujici jmeno adresare
fi
elif [[ "$1" =~ [-*] ]]
then
echo "Invalid option!" >/dev/stderr
exit 64
else
echo "Unrecognized action!" >/dev/stderr
exit 67
fi
elif [ $# -eq 3 ] #zadany tri pozicni parametry
then
echo "Zadany 3 pozicni parametry..."
if [ "$1" = "-i" ]
then
if [ -n "$2" ] #test, zda je retezec za prepinacem nenulovy
then
if [ "$2" = "/" ]
then
echo "Ignored-files list should not include a name of the root directory!" >/dev/stderr
exit 64
else
if [ "$3" = "-n" ]
then
echo "Root directory: $PWD"
count_directories "$PWD" "$2"
echo "Navratovy kod: $?"
count_files "$PWD" "$2"
echo "Navratovy kod: $?"
make_fhist "$PWD" $3 "$2"
echo "Navratovy kod: $?"
exit $?
elif [ -d "$3" ]
then
echo "Root directory: $3"
count_directories "$3" "$2"
echo "Navratovy kod: $?"
count_files "$3" "$2"
echo "Navratovy kod: $?"
make_fhist "$3" x "$2"
echo "Navratovy kod: $?"
exit $?
else
echo "Not a valid parameter!" >/dev/stderr
exit 64 # 64 - spatny parametr
fi
fi
else
echo "Valid regular expression must follow!" >/dev/stderr
exit 65 # 65- nepresny nebo nulovy regularni vyraz
fi
elif [[ "$1" =~ [-*] ]]
then
echo "Invalid option!" >/dev/stderr
exit 64
else
echo "Wrong order of parameters, correct form is: dirgraph [-i FILE_ERE] [-n] [DIR]" >/dev/stderr
exit 67
fi
elif [ $# -eq 4 ]
then
echo "Zadany 4 pozicni parametry..."
if [ "$1" = "-i" ]
then
if [ -n "$2" ] #test, zda je retezec za prepinacem nenulovy
then
if [ "$2" = "/" ] #nesmi pokryvat nazev korenoveho adresare
then
echo "Ignored-files list should not include a name of the root directory!" >/dev/stderr
exit 64
else
if [ "$3" = "-n" ]
then
if [ -d "$4" ]
then
echo "Root directory: $4"
count_directories "$4" "$2"
echo "Navratovy kod: $?"
count_files "$4" "$2"
echo "Navratovy kod: $?"
make_fhist "$4" $3 "$2"
echo "Navratovy kod: $?"
exit $?
else
echo "$4: Not a name of a directory!" >/dev/stderr
exit 66
fi
else
echo "$3: Wrong parameter!" >/dev/stderr
exit 64
fi
fi
else
echo "Valid regular expression must follow!" >/dev/stderr
exit 65
fi
elif [[ "$1" =~ [-*] ]]
then
echo "Invalid option!" >/dev/stderr
exit 64
else
echo "Wrong order of parameters, correct form is: dirgraph [-i FILE_ERE] [-n] [DIR]" >/dev/stderr
exit 67
fi
else
echo "Too much parameters!" >/dev/stderr
exit 67
fi
exit 1 #dostat se sem je chyba!