-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.sh
More file actions
614 lines (543 loc) · 14.2 KB
/
misc.sh
File metadata and controls
614 lines (543 loc) · 14.2 KB
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
603
604
605
606
607
608
609
610
611
612
613
614
sum() {
echo $(cat "${@--}") | tr -s ' ' '+' | bc
}
# Find
_find_sorted() {
# General-purpose find and sort
format="$1"
sort_type="$2"
shift 2
while IFS=$'\t' read -r -d '' -u 9 stat path
do
printf '%s\0' "$path"
done 9< <(find ${1+"$@"} -printf "$format"'\t%p\0' | sort --key 1 "$sort_type" --zero-terminated)
}
find_date_sorted() {
# Sorted by ISO date, ascending
# Separated by NUL to be able to reuse in other loops
_find_sorted '%TY-%Tm-%TdT%TH:%TM:%TS' '--general-numeric-sort' "$@"
}
find_size_sorted() {
_find_sorted '%s' '--numeric-sort' "$@"
}
substring() {
# Extract substring with positive or negative indexes
# @param $1: String
# @param $2: Start (default start of string)
# @param $3: Length (default until end of string)
local -i strlen="${#1}"
local -i start="${2-0}"
local -i length="${3-${#1}}"
if [[ "$start" -lt 0 ]]
then
let start+=$strlen
fi
if [[ "$length" -lt 0 ]]
then
let length+=$strlen
let length-=$start
fi
if [[ "$length" -lt 0 ]]
then
return
fi
printf %s "${1:$start:$length}"
}
path_common() {
if [[ $# -ne 2 ]]
then
return 2
fi
# Remove repeated slashes
for param
do
param="$(printf %s. "$1" | tr -s "/")"
set -- "$@" "${param%.}"
shift
done
common_path="$1"
shift
for param
do
while case "${param%/}/" in "${common_path%/}/"*) false;; esac; do
new_common_path="${common_path%/*}"
if [[ "$new_common_path" = "$common_path" ]]
then
return 1 # Dead end
fi
common_path="$new_common_path"
done
done
printf %s "$common_path"
}
# Bash
bash_timeout() {
# Set the idle timeout before the shell will be closed automatically
# @param $1: Timeout in <N>d<N>h<N>m<N>[s] format
local timeout="$1"
timeout="${timeout//d/*24h}"
timeout="${timeout//h/*60m}"
timeout="${timeout//m/*60}"
timeout="${timeout//s/}"
export TMOUT="$(($timeout))"
}
# Perl
perl_module_version() {
local version
for module
do
version="$(perl -M${module} -e "print \$${module}::VERSION")"
if [[ -n "$version" ]]
then
echo "$module $version"
else
echo "Unknown module $module" >&2
return 1
fi
done
}
perl_module_files() {
perl -MFile::Find=find -MFile::Spec::Functions -Tlwe 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC'
}
perl_modules() {
perl -MExtUtils::Installed -e'my $inst = ExtUtils::Installed->new();print $_, $/ for $inst->modules'
}
perl_modules_test() {
local -i exit_code=0
while IFS= read -r -u 9
do
perl_module_version "$REPLY" 2>&1 >/dev/null || let exit_code=1
done 9< <( perl_modules )
return $exit_code
}
# String handling
shortest() {
# Print only the shortest line
# If multiple lines match, only the first one will be printed
awk '(NR == 1 || length < length(line)) { line = $0 } END { print line }'
}
longest() {
# Print only the longest line
# If multiple lines match, only the first one will be printed
awk '(NR == 1 || length > length(line)) { line = $0 } END { print line }'
}
longer() {
# Print any lines in $2... which are more characters than $1.
#
# Example:
#
# $ longer 80 foo.txt
# Print lines in foo.txt which are more than 80 characters long
if [[ $# -lt 2 ]]
then
echo 'Synopsis: longer NUMBER FILE...' >&2
return 2
fi
local -ri length="$1"
shift
local -i line_number
for path
do
line_number=1
while IFS= read -r -d $'\n' || [[ -n "$REPLY" ]]
do
if [[ ${#REPLY} -gt $length ]]
then
printf %q "$path" : $line_number :... "${REPLY:${length}}"
printf '\n'
fi
let line_number++
done < "$path"
done
}
exclude_cvs() {
grep "$@" -Fve '/CVS/'
}
exclude_svn() {
grep "$@" -Fve '/.svn/'
}
exclude_git() {
grep "$@" -Fve '/.git/'
}
exclude_vcs() {
exclude_cvs "$@" | exclude_svn "$@" | exclude_git "$@"
}
# GNU Make
make_targets() {
targets="$(make --print-data-base --question | grep '^[^.%][-A-Za-z0-9_]*:' | cut -d : -f 1 | sort -u)"
longest_line="$(longest <<< "$targets")"
columns=$(bc <<< "${COLUMNS-80} / (${#longest_line} + 1)")
pr --omit-pagination --width=${COLUMNS-80} --columns=$columns <<< "$targets"
}
locale_value() {
# Return the highest priority language variable available
# Based on http://mywiki.wooledge.org/BashFAQ/098
# To print which variable was used, uncomment the `echo` line
# $1: Optional locale category variable name (see `man locale`)
local varname=LANG # Fallback
if [[ "${1-}" =~ ^LC_[A-Z]+$ ]] && declare -p "${1-}" >/dev/null 2>&1
then
varname="$1"
fi
if [[ -n "${LC_ALL+defined}" ]]
then
varname=LC_ALL
fi
if [[ -n "${LANGUAGE+defined}" ]] && [[ "${LANG-}" != C ]]
then
varname=LANGUAGE
fi
#echo "$varname" >&2
echo "${!varname}"
}
schroedinger() {
# Succeed or fail randomly
return $((RANDOM%2))
}
trs() {
# Translate strings
# $1: Original string
# $2, $4, ...: Search strings
# $3, $5, ...: Replacement strings
local string="$1"
shift
while [[ $# -gt 0 ]]
do
string="$(sed -e "s/$1/$2/g" <<< "$string")"
shift 2
done
printf "$string"
}
zsed() {
# Replace within tarballs
# $1: Replacement string
# $2...: tar gzip files
if [[ "${2+defined}" != defined ]]
then
return 2
fi
local replacement="$1"
shift
local -i exit_code
for path
do
if [[ ! -r "${path}" ]]
then
exit_code=2
continue
fi
full_pathx="$(readlink -fn -- "$path"; echo x)"
full_path="${full_pathx%x}"
tmp="$(mktemp -d)"
cd "$tmp"
tar -xzf "$full_path"
while IFS= read -r -d '' -u 9
do
sed -i -e "$replacement" "$REPLY"
done 9< <( find . -type f -exec printf '%s\0' {} + )
tar -czf "$full_path" .
cd - >/dev/null
done
return ${exit_code-0}
}
collapse() {
# Collapse lines based on first column
local name old_name value
local first=1
while read -r name value
do
[[ "$name" != "${old_name-}" ]] && [[ "${first+defined}" != defined ]] && printf "${IFS: -1}"
[[ "$name" != "${old_name-}" ]] && printf %s "$name"
[[ -n "$value" ]] && printf %s "${IFS:0:1}"
printf %s "$value"
old_name="$name"
unset first
done
if [[ "$name" ]]
then
[[ "$name" != "${old_name-}" ]] && [[ "${first+defined}" != defined ]] && printf "${IFS: -1}"
[[ "$name" != "${old_name-}" ]] && printf %s "$name"
[[ -n "$value" ]] && printf %s "${IFS:0:1}"
printf %s "$value"
else
[[ "${first+defined}" != defined ]] && printf "${IFS: -1}"
fi
}
empty_line_before_eof() {
# Insert a newline after the last line if it's not empty.
# Note that this means that the empty file will *not* be changed (it
# already ends with an empty line).
#
# @param $1...: Sed options and/or input files
#
# Example:
#
# $ empty_line_before_eof -i .bak ./*
# Save backups to filename.bak, and process each file
#
# Command breakdown:
# '$' denotes the end of file
# 'a\' appends the following text (which is nothing, in this case) on a new line
# In other words, if the last line contains a character that is not newline,
# append a newline.
sed -e '$a\' "$@"
}
markdown_page() {
# Add stuff to markdown output to make it valid XHTML 1.0 Strict with
# unambiguous encoding.
# @param $1...: markdown input files
cat <<EOF
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Markdown</title>
</head>
<body>
EOF
for path
do
markdown "$path"
done
cat <<EOF
</body>
</html>
EOF
}
valid_ipv4() {
declare -i quads=0
while IFS= read -r quad
do
let quads++
while [[ $quad = 0* ]]
do
quad=${quad#0}
done
if [[ $quads -gt 4 || ! "${quad:-0}" =~ [0-9]+ || "${quad:-0}" -lt 0 || "${quad:-0}" -gt 255 ]]
then
return 1
fi
done < <(echo "$1" | tr '.' '\n')
if [[ ! $quads -eq 4 ]]
then
return 1
fi
}
completions() {
# Print autocompletions
# Examples:
#
# $ completions
# List all available commands:
#
# $ completions git config '' | grep user
# List all user-related Git configuration settings:
if [[ $# -eq 0 ]]
then
set -- ''
fi
local COMP_LINE="$*"
local COMP_WORDS=("$@")
local COMP_CWORD=${#COMP_WORDS[@]}
((COMP_CWORD--))
local COMP_POINT=${#COMP_LINE}
local COMP_TYPE=9
local COMP_KEY=9
local COMP_WORDBREAKS='"'"'><=;|&(:"
local COMPREPLY
_command_offset 0
for result in "${COMPREPLY[@]}"
do
echo "$result"
done
}
dpkg-quilt() {
quilt --quiltrc="${HOME}/.quiltrc-dpkg" ${@+"$@"}
}
fullname() {
# The fifth field contains the name
# The first subfield contains the full name
#
# Examples:
#
# $ fullname
# Print the current user's full name
#
# $ fullname username
# Print user "username"'s full name
getent passwd -- "${@-$USER}" | cut -d ':' -f 5 | cut -d ',' -f 1
}
watch_url() {
# Watch a URL for changes, and open it in a web browser when a change is
# detected.
# @param $1: URL
# @param $2: Optional replacements in sed style, e.g.
# /foo/d;s/123/456/g;...
# @param $3: Interval between checks; by default 1 hour
if [[ $# -eq 0 ]] || [[ $# -gt 3 ]]
then
echo 'Synopsis: watch_url URL [REPLACEMENTS] [INTERVAL]' >&2
return 2
fi
local -r url="$1"
local -r dir="$(mktemp --directory)"
local -r file="${dir}/index.html"
# Get the current document
wget --quiet --output-document "$file" "$url"
# Replace in the current document
sed -i -e "${2-}" "${file}"
# Check for differences at intervals
# Add `--quiet` to the `diff` command if you don't want to see the
# difference.
while diff "$file" <(wget --quiet --output-document - "$url" | sed -e "${2-}")
do
sleep "${3-1h}"
done
# Cleanup
rm -- "$file"
rmdir -- "$dir"
# Start browser
x-www-browser "$url"
}
coalesce() {
# Print the first non-empty line
# Returns success if a non-empty line is found.
#
# Examples:
#
# $ coalesce < test.sh
# #!/usr/bin/env bash
#
# $ var=('' 'foo' 'bar')
# $ printf '%s\n' "${var[@]}" | coalesce
# foo
while IFS= read -r || [[ -n "$REPLY" ]]
do
if [[ -n "$REPLY" ]]
then
printf '%s\n' "$REPLY"
return
fi
done
return 1
}
head_tail() {
# Both head and tail of a file
# @param $1...$#-1: head *and* tail options, such as "-n 5"
# @param $#: File name
# Remove last parameter
local index=$#
for param
do
shift
if [[ $index -ne 1 ]]
then
set -- "$@" "$param"
fi
index=$((index - 1))
done
{
head "$@";
tail "$@";
} < "$last"
}
log_time_diff() {
# Prepends syslog log lines with the difference between the current line
# and the previous one in seconds
#
# Examples:
#
# $ log_time_diff < /var/log/syslog
#
# $ log_time_diff < /var/log/syslog | sort --numeric --key=1
local prev line month day time rest
while read -r line
do
read -r month day time rest <<< "$line"
ts="$(date --date="$month $day $time" +%s)"
if [[ "${prev:+set}" = 'set' ]]
then
diff="$((ts - prev))"
else
diff=0
fi
printf "%d %s\n" "$diff" "$line"
prev="$ts"
done
}
escaped_declare() {
# Print shell-escaped variable declarations
#
# This makes it possible to copy-paste the declarations verbatim and get
# the same values.
(
eval '
declare() {
printf declare;
printf " %q" "$@";
printf "\\n";
}'"
$(declare -p)"
)
}
date_range() {
# Print dates from $1 through $2, inclusive
#
# Both values default to today.
#
# Examples:
#
# $ date_range 2000-01-01 2000-12-31
# Print all the dates in year 2000
#
# $ date_range 'last Monday'
# Print the dates from last Monday to today, inclusive
start="$(date --rfc-3339=date --date="${1-today}")"
end="$(date --rfc-3339=date --date="${2-today}")"
while [[ "$start" < "$end" || "$start" = "$end" ]]
do
printf '%s\n' "$start"
start="$(date --date="$start + 1 day" +%Y-%m-%d)"
done
}
insert_after_last() {
# Insert a string after the last occurrence of a pattern in files
#
# @param $1: Gawk-compatible pattern
# @param $2: Text to append
# @param $3...$#: Paths
#
# Example:
#
# $ insert_after_last '^[ \t]*[^# \t]' '# My comment' /path
# Insert the line '# My comment' after the last comment
if [[ $# -lt 3 ]]
then
echo "Synopsis: ${FUNCNAME} pattern text file..." >&2
return 2
fi
local -r pattern=$1
local -r text=$2
shift 2
for path
do
if [[ ! -e "$path" ]]
then
echo "${FUNCNAME}: $path: No such file" >&2
return 1
fi
set -o pipefail
tac -- "$path" |
TEXT=$text gawk -v size="$(wc -c < "$path")" '
$0 !~ /'"$pattern"'/ {
footer=$0 "\n" footer
next
}
{
system("dd bs=1 seek=" size - length(footer) " conv=notrunc if=/dev/null 2>/dev/null")
printf "%s\n%s", ENVIRON["TEXT"], footer
exit
}' 1<> "$path"
done
}