-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.shlib
602 lines (502 loc) · 13.9 KB
/
.shlib
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
#!/bin/bash
###############################################################################
# Functions #
###############################################################################
if [ -d "${HOME}/.local" ] && [ -e "${HOME}/.local/func.${USER}.${hostname}" ] ; then
source "${HOME}/.local/func.${USER}.${hostname}" > /dev/null 2>&1
fi
if [ -d "${HOME}/.local" ] && [ -e "${HOME}/.local/func.all" ] ; then
source "${HOME}/.local/func.all" > /dev/null 2>&1
fi
function random_bytes {
local help=0
local usage=0
local OPTARG=
local OPTIND=
local args="b:huw:"
local bytes=16
local width=0
local dev="/dev/random"
while getopts "${args}" option ; do
case $option in
b) bytes="${OPTARG}" ;;
h) help=1 ;;
u) dev="/dev/urandom" ;;
w) width="${OPTARG}" ;;
esac
done
if [ ${help} -eq 0 ] ; then
if [ 0 -ge ${bytes} -o 0 -gt ${width} ] ; then
echo "The Bytes (${bytes}) and Width (${width}) fields"
echo "MUST be non-negative, and Bytes must be positive"
usage=1
fi
fi
if [ ${help} -eq 1 -o ${usage} -eq 1 ] ; then
cat << USAGE
${FUNCNAME} [ -h ] [ -b <bytes> ] [ -w <width> ]
Generates random bytes as a single string.
-b <bytes>: defaults to 16
-h: print this usage statement
-u: use the non-blocking urandom device
-w <width>: prints in a block of width <width> (0 means one-line)
USAGE
[ ${help} -eq 1 ] && return 0 || return -1
fi
# gather the raw bytes
local bpl=16
local lines=$(( ${bytes}/${bpl} ))
local rem=$(( ${bytes}%${bpl} ))
[ ${rem} -ne 0 ] && (( lines++ ))
local l_a="$((${lines}+1))"
local l_b="$((${lines}+2))"
local hex="$(dd if=${dev} bs=${bytes} count=1 2>/dev/null | hexdump -C | sed -e ${l_a},${l_b}\ d | cut -b 10-58 | tr '\n' ' ' | sed -e 's/ //g')"
if [ ${width} -eq 0 ] ; then
echo ${hex}
else
while [ -n "${hex}" ] ; do
local part="$(echo ${hex} | dd bs=${width} count=1 2>/dev/null)"
echo "${part}"
hex="$(echo ${hex} | dd bs=1 skip=${width} 2>/dev/null)"
done
fi
}
function mk {
if [ -n "$2" ] ; then
awk -F$2 "{print \$$1}"
else
awk "{print \$$1}"
fi
}
function clean {
local backup=1
local python=0
local error=1
local help=0
local print=1
local noexec=0
local path="."
local recursive=0
local usage=0
local OPTARG=
local OPTIND=
local args="behnP:pqr"
while getopts "${args}" option ; do
case $option in
b) error=0 ;;
e) backup=0 ;;
h) help=1 ;;
n) noexec=1 ;;
P) path="${OPTARG}" ;;
p) python=1 ;;
q) print=0 ;;
r) recursive=1 ;;
esac
done
if [ ${help} -ne 1 ] ; then
if [ ${noexec} -eq 1 -a ${print} -ne 1 ] ; then
echo "Quiet and Print-Only cannot be used at the same time."
usage=1
fi
if [ ${error} -ne 1 -a ${backup} -ne 1 ] ; then
echo "Error-Only and Backup-Only cannot be used at the same time."
usage=1
fi
if [ ! -d ${path} ] ; then
echo "Path does not exist."
usage=1
fi
fi
if [ ${help} -eq 1 -o ${usage} -eq 1 ] ; then
cat << USAGE
${FUNCNAME} [ -q ] [ -p ] [ -h ] [ -r ] [ -e ] [ -b ] [ -n ] [ -P <path> ]
Cleans the emacs backup and error files.
-b: backup files only
-e: error files only
-h: print this usage statement
-p: clean up python files as well
-n: do not execute, just print
-P <path>: Use the specified path (defaults to '.')
-q: execute only, do not print
-r: recursively clean
USAGE
[ ${help} -eq 1 ] && return 0 || return -1
fi
local cmd="find ${path}"
if [ ${recursive} -ne 1 ] ; then
cmd="${cmd} -maxdepth 1"
fi
cmd="${cmd} -type f"
local files="$($cmd)"
regex="^"
if [ ${backup} -eq 1 -a ${error} -eq 1 ] ; then
regex="${regex}.*~|#.*#"
elif [ ${backup} -eq 1 ] ; then
regex="${regex}.*~"
else
regex="${regex}#.*#"
fi
if [ ${python} -eq 1 ] ; then
regex="${regex}$|.*\.pyc$|.*\.pyo$"
fi
for f in $files ; do
[ $(echo "${f}" | egrep "${regex}") ] || continue
if [ ${print} -eq 1 ] ; then
echo "${f}"
fi
if [ ${noexec} -ne 1 ] ; then
yes | /bin/rm -f "${f}" > /dev/null 2>&1
fi
done
return 0
}
function sanitize_verbosity {
local verb=$1
case "${verb}" in
0) verb=0 ;;
1) verb=1 ;;
2) verb=2 ;;
*) verb=1 ;;
esac
[ -n "${__g_verb}" ] && verb="${__g_verb}"
echo ${verb}
}
function fulldir {
local start=$1
local dir="$(dirname ${start})"
if [ -z "${dir}" ] ; then
pwd
elif [ "${dir:0:1}" == "/" ] ; then
echo "${dir}"
else
echo "$(pwd)/${dir}"
fi
return 0
}
function do_exec_start {
local msg=$1
local verb="$(sanitize_verbosity $2)"
if [ ${verb} -ge 2 ] ; then
printf " %s" "${msg}"
elif [ ${verb} -gt 0 ] ; then
printf " %s..." "${msg}"
fi
}
function do_exec_success {
printf "Success\n"
}
function do_exec_failure {
printf "Failure\n"
}
function do_exec_end {
local ret=$1
local verb="$(sanitize_verbosity $2)"
if [ ${ret} -eq 0 ] ; then
[ ${verb} -gt 0 ] && do_exec_success
return 0
else
[ ${verb} -gt 0 ] && do_exec_failure
return 1
fi
}
function do_exec {
local cmd=$1
local msg=$2
local verb="$(sanitize_verbosity $3)"
do_exec_start "${msg}" ${verb}
if [ ${verb} -ge 2 ] ; then
echo -n " (${cmd})..."
if [ ${verb} -ge 3 ] ; then
${cmd}
else
${cmd} > /dev/null 2>&1
fi
else
${cmd} > /dev/null 2>&1
fi
do_exec_end $? ${verb}
return $?
}
function n_threads {
local n="$(cat /proc/cpuinfo | grep ^processor | wc -l)"
[ $? -eq 0 ] || return 1
(( n = (n * 2) + 1 ))
echo "${n}"
return 0
}
function is_64 {
if [ -n "$(uname -a | grep -i x86_64)" ] ; then
return 0
else
return 1
fi
}
function rand_bytes {
# GetOpt vars
local help=0
local usage=0
local OPTARG=
local OPTIND=
local args="b:dhrw:"
# input vars
local bytes=4
local width=8
local decimal=0 # unimplemented
local dev="/dev/urandom"
while getopts "${args}" option ; do
case $option in
b) bytes="${OPTARG}" ;;
d) decimal=1 ;;
h) help=1 ;;
r) dev="/dev/random" ;;
w) width="${OPTARG}" ;;
esac
done
if [ ${help} -ne 1 ] ; then
if [ ${decimal} -eq 1 ] ; then
echo "We don't currently support the decimal-only version"
usage=1
fi
if [ ${width} -lt 0 ] ; then
echo "Widths should be positive"
usage=1
fi
if [ ${bytes} -lt 0 ] ; then
echo "Bytes should be positive"
usage=1
fi
fi
if [ ${help} -eq 1 -o ${usage} -eq 1 ] ; then
cat << USAGE
${FUNCNAME}
[ -b <n-bytes> ]
[ -d ]
[ -h ]
[ -r ]
[ -w <width> ]
Displays <n-bytes> random bytes to the screen, <width> bytes per line.
-b <n-bytes>: default is 4
-d: not-implemented (decimal)
-h: displays this help message
-w <width>: default is 8
-r: use /dev/random rather than /dev/urandom
USAGE
[ ${help} -eq 1 ] && return 0 || return -1
fi
# gather the raw hex data
local raw_lines=$(dd if=/dev/urandom bs=${bytes} count=1 2>/dev/null | hexdump -C | sed -e 's/ //g' | awk -F\| '{print $1}' | cut -b 9-40)
local raw=""
for line in $raw_lines ; do
raw="${raw}${line}"
done
# format the output
if [ ${width} -eq 0 ] ; then
echo "${raw}"
else
while [ -n "${raw}" ] ; do
echo "${raw}" | cut -b 1-${width}
local start=$(( ${width}+1 ))
local end=$(( ${bytes}*2 ))
if [ ${start} -lt ${end} ] ; then
raw="$(echo ${raw} | cut -b ${start}-${end})"
else
raw=""
fi
done
fi
}
if [ -f "${HOME}/.git.sh" ] ; then
. "${HOME}/.git.sh"
fi
function _optcomplete()
{
COMPREPLY=( \
$( \
COMP_LINE=$COMP_LINE \
COMP_POINT=$COMP_POINT \
COMP_WORDS="${COMP_WORDS[*]}" \
COMP_CWORD=$COMP_CWORD \
OPTPARSE_AUTO_COMPLETE=1 \
$1 \
) \
)
}
complete -F _optcomplete ec2
function uuid()
{
echo 'require "securerandom" ; SecureRandom.uuid' | irb | tail -n 2 | head -n 1 | sed -e 's/"//g'
}
function canon_dirname {
# if we pass in nothing, spit out nothing
[ -z "${1}" ] && return
# dirname isn't smart enough to check if the argument is already a
# directory, it just gives you one up (unless you give it /, of
# course)
local dir=$(dirname "$1")
# initially, we assume that the argument is a file path, not a
# directory path. We therefore assume that we will have to change
# to the dirname from above to discover the canonical directory
# path
local tgt="${dir}"
# if we are proven wrong, and have been passed a directory, then
# the target is adjusted accordingly
if [ -d "${1}" ] ; then
tgt="$1"
fi
# now that we know where to go, we go there, echo the canonical
# path, and go back
pushd "${tgt}" > /dev/null 2>&1
pwd
popd > /dev/null 2>&1
}
function canon_dirname {
# if we pass in nothing, spit out nothing
[ -z "${1}" ] && return
# dirname isn't smart enough to check if the argument is already a
# directory, it just gives you one up (unless you give it /, of
# course)
local dir=$(dirname "$1")
local base=$(basename "$1")
# initially, we assume that the argument is a file path, not a
# directory path. We therefore assume that we will have to change
# to the dirname from above to discover the canonical directory
# path
local tgt="${dir}"
# if we are proven wrong, and have been passed a directory, then
# the target is adjusted accordingly
if [ -d "${1}" ] ; then
tgt="$1"
fi
# now that we know where to go, we go there, echo the canonical
# path, and go back
pushd "${tgt}" > /dev/null 2>&1
pwd
popd > /dev/null 2>&1
}
function canon_path {
if [ -d "$1" ] ; then
canon_dirname "$@"
else
echo $(canon_dirname "$@")/$(basename "$@")
fi
}
function read_y_n {
local result=1
while true ; do
read -p "$1 [Yy/Nn]? " -n 1
echo
if [[ $REPLY =~ ^[YyNn]$ ]] ; then
[[ $REPLY =~ ^[Yy]$ ]] && result=0
break
else
echo "Please respond with [YyNn]"
fi
done
[ ${result} -eq 0 ] && return 0 || return 1
}
function sd_flash {
local help=0
local usage=0
local OPTARG=
local OPTIND=
local args="h"
while getopts "${args}" option ; do
case $option in
h) help=1 ;;
esac
done
# Gather the image and device arguments
local img="${@:$OPTIND:1}"
shift 1
local dev="${@:$OPTIND:1}"
[ -n "${dev}" ] && dev="$(canon_path ${dev})"
if [ ${help} -eq 0 ] ; then
if [ ! -e "${dev}" ] ; then
echo "Missing valid device: >${dev}<"
usage=1
fi
if [ ! -f "${img}" ] ; then
echo "Missing valid image: >${img}<"
usage=1
fi
local root_dev="$(mount | grep ' / ' | fmt -1 | head -n 1)"
# just in case we're on mac and using rdisk instead of disk
local test_dev="$(echo ${dev} | sed -e 's/rdisk/disk/g')"
echo ${root_dev} | grep -q ${test_dev}
if [ $? -eq 0 ] ; then
echo "DANGER BILL WILLIAMSON"
echo "Refusing to flash what looks like the root dev"
echo "Root FS Dev: ${root_dev}"
usage=1
fi
# If we're on mac, make sure we're using rdisk, not disk
echo $OSTYPE | grep -q darwin*
if [ $? -eq 0 ] ; then
if [ -n "${dev}" ] ; then
if [[ "$(basename ${dev})" != "rdisk*" ]] ; then
echo "${dev}" | egrep -q '^/dev/rdisk[0-9]+$'
if [ $? -ne 0 ] ; then
echo "You look like you're on a MAC, but not using /dev/rdisk*"
usage=1
fi
fi
fi
fi
fi
if [ ${help} -eq 1 -o ${usage} -eq 1 ] ; then
cat << USAGE
${FUNCNAME} [ -h ] <image> <device>
Flashes an SD Card
-h: print this usage statement
USAGE
[ ${help} -eq 1 ] && return 0 || return -1
fi
# get sudo
echo " Obtaining sudo"
sudo echo -n
[ $? -eq 0 ] || return
# umount everything
echo " Unmounting Partitions"
while mount | grep -q "^${test_dev}" ; do
if [ $? -eq 0 ] ; then
local mnt=$(mount \
| $(which grep) "^${test_dev}" \
| awk -F\( {'print $1'} \
| cut -f 3- -d\ \
| sed -e 's/[[:space:]]*$//' \
| head -n 1 \
| rev \
| cut -f 3- -d\ \
| rev)
echo "FS Currently Mounted on Target Disk: " $mnt
read_y_n "Unmount and Continue"
if [ $? -eq 0 ] ; then
case ${OSTYPE} in
darwin*)
diskutil umount "${mnt}"
;;
*)
sudo umount "${mnt}"
;;
esac
[ $? -ne 0 ] && return 1
else
echo "Aborting"
return 0
fi
fi
done
echo " Flashing Image"
sudo dd if=${img} of=${dev} bs=$((2**20))
}
function ccount {
file="$1"
local color="$(gs -o - -sDEVICE=inkcov $1 | grep -v "^ 0.00000 0.00000 0.00000" | grep "^ " | wc -l)"
local total="$(pdfinfo $1 | grep Pages:)"
printf "%4d / %-4d Color Pages" "${color}" "{total}"
# for page in $(identify -density 12 -format '%p ' "$file") ; do
# if convert "$file[$((page-1))]" -colorspace RGB -unique-colors txt:- | sed -e 1d | egrep -q -v ': \(\s*([0-9]*),\s*\1,\s*\1' ; then
# echo $page
# fi
# done
}