-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripting-utils.sh
executable file
·74 lines (63 loc) · 2.06 KB
/
scripting-utils.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
#!/usr/bin/env bash
#
# PATH MANAGEMENT
#
make_clean_PATH() {
local my_path=${@:-"${PATH}"}
printf "%s\n" ${my_path} |
# un-form PATH-like strings
tr ':' '\n' |
# De-duplicate. # ref: https://stackoverflow.com/a/20639730
# Or use awk '!x[$0]++', as explained here https://stackoverflow.com/a/11532197
cat -n | sort -uk2 | sort -nk1 | cut -f2- |
# re-form lines into single-colon-separated PATH-like string
tr '\n' ':' | tr -s ':' |
# ensure no trailing colons
sed 's/:$//g'
}
# DEPENDENCY CHECKS
#
ensure_deps() {
# Given a list of dependencies, emit unmet dependencies to stderr,
# and return 1. Otherwise silently return 0.
local required_deps="${@}"
local err_code=0
for prog in ${required_deps}
do if ! which "$prog" > /dev/null
then 2>& printf "%s\n" "$prog"
err_code=1
fi
done
return ${err_code}
}
ensure_min_bash_version() {
# Given a 'Major.Minor.Patch' SemVer number, return 1 if the system's
# bash version is older than the given version. Default to 4.0.0.
# Hat tip: https://unix.stackexchange.com/a/285928
local semver="${1:-4.0.0}"
local bashver="${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]}.${BASH_VERSINFO[2]}"
[[ $(printf "%s\n" ${semver} ${bashver} | sort -V | head -1) == ${semver} ]]
}
#
# FUNCTION QUERIES and TRANSFORMS
#
fn_to_sh_syntax() {
# Given a zsh-style or bash-style function definition, emit
# an sh-style companion. I prefer to keep the opening brace
# on the same line as the fn name and the body+closing braces
# on the following lines, for cleaner regex-matching of fn
# definitions. e.g.
#
# 'function foo3_bar() {' --> 'foo3_bar() {'
# 'function foo3_bar {' --> 'foo3_bar() {'
#
sed -E 's;(function)\s+(\w+)(\(\))?+\s+\{;\2() \{;' ;
}
fn_ls() {
# List out names of functions found in the given files,
# assuming well-formed function definitions, written as:
#
# 'foo3_bar() {'
#
grep -E -o "^(\w+)\(\)\s+\{" "$@" | tr -d '(){'
}