Revision 9226
Added by Aaron Marcuse-Kubitza over 11 years ago
lib/sh/util.sh | ||
---|---|---|
36 | 36 |
shopt -s expand_aliases |
37 | 37 |
|
38 | 38 |
|
39 |
#### vars |
|
40 |
|
|
41 |
set_var() { eval "$1"'="$2"'; } |
|
42 |
|
|
43 |
set_inv() { set_var no_"$1" "$(test "${!1}" || echo 1)"; } |
|
44 |
|
|
45 |
# usage: local var=...; local_inv |
|
46 |
alias local_inv='declare "no_$var=$(test "${!var}" || echo 1)"' |
|
47 |
|
|
48 |
get_prefix_vars() { : "${prefix:?}"; eval echo '${!'$prefix'*}'; } |
|
49 |
|
|
50 |
# usage: local prefix=..._; import_vars |
|
51 |
# when used inside another alias 2+ levels deep, *must* be run inside a function |
|
52 |
alias import_vars="$(cat <<'EOF' |
|
53 |
: "${prefix:?}" |
|
54 |
declare src_var dest_var |
|
55 |
for src_var in $(get_prefix_vars); do |
|
56 |
dest_var="${src_var#$prefix}" |
|
57 |
declare "$dest_var=${!src_var}"; echo_vars "$dest_var" |
|
58 |
done |
|
59 |
EOF |
|
60 |
)" |
|
61 |
|
|
62 |
|
|
39 | 63 |
#### aliases |
40 | 64 |
|
41 | 65 |
unalias() { builtin unalias "$@" 2>&- || true; } # no error if undefined |
... | ... | |
346 | 370 |
if test "$explicit_errors_only"; then disable_logging; fi |
347 | 371 |
|
348 | 372 |
|
349 |
#### vars |
|
350 |
|
|
351 |
set_var() { eval "$1"'="$2"'; } |
|
352 |
|
|
353 |
set_inv() { set_var no_"$1" "$(test "${!1}" || echo 1)"; } |
|
354 |
|
|
355 |
# usage: local var=...; local_inv |
|
356 |
alias local_inv='declare "no_$var=$(test "${!var}" || echo 1)"' |
|
357 |
|
|
358 |
get_prefix_vars() { : "${prefix:?}"; eval echo '${!'$prefix'*}'; } |
|
359 |
|
|
360 |
# usage: local prefix=..._; import_vars |
|
361 |
# when used inside another alias 2+ levels deep, *must* be run inside a function |
|
362 |
alias import_vars="$(cat <<'EOF' |
|
363 |
: "${prefix:?}" |
|
364 |
declare src_var dest_var |
|
365 |
for src_var in $(get_prefix_vars); do |
|
366 |
dest_var="${src_var#$prefix}" |
|
367 |
declare "$dest_var=${!src_var}"; echo_vars "$dest_var" |
|
368 |
done |
|
369 |
EOF |
|
370 |
)" |
|
371 |
|
|
372 |
|
|
373 | 373 |
#### functions |
374 | 374 |
|
375 | 375 |
func_exists() { declare -f "$1" >/dev/null; } |
Also available in: Unified diff
lib/sh/util.sh: moved vars section to beginning so other sections can use it (it does not contain logging statements, so it doesn't need to be after verbose output)