Revision 13229
Added by Aaron Marcuse-Kubitza over 10 years ago
trunk/lib/sh/util.sh | ||
---|---|---|
199 | 199 |
|
200 | 200 |
#### functions |
201 | 201 |
|
202 |
func_exists() { declare -f "$1" >/dev/null; } |
|
203 |
|
|
202 | 204 |
kw_params() # usage: func() { kw_params param_var...; }; ...; param_var=... cmd |
203 | 205 |
# removes keyword-param-only vars from the environment |
204 | 206 |
{ unexport "$@"; } |
... | ... | |
223 | 225 |
all_funcs() # usage: for func in $(all_funcs); do ...; done # all declared funcs |
224 | 226 |
{ declare -F|while read -r line; do echo -n "${line#declare -f } "; done; } |
225 | 227 |
|
228 |
copy_func() # usage: from=... to=... copy_func |
|
229 |
# $to must not exist. to get around the no-clobber restriction, use `unset -f`. |
|
230 |
{ |
|
231 |
: "${from:?}" "${to:?}" |
|
232 |
func_exists "$from" || die "function does not exist: $from" |
|
233 |
! func_exists "$to" || die "function already exists: $to" |
|
234 |
local from_def="$(declare -f "$from")" |
|
235 |
eval "$to${from_def#$from}" |
|
236 |
} |
|
226 | 237 |
|
238 |
func_override() # usage: func_override old_name__suffix |
|
239 |
{ from="${1%__*}" to="$1" copy_func; } |
|
240 |
|
|
241 |
ensure_nested_func() # usage: func__nested_func() { ensure_nested_func; ... } |
|
242 |
{ |
|
243 |
local nested_func="${FUNCNAME[1]}" |
|
244 |
local func="${nested_func%%__*}" |
|
245 |
contains "$func" "${FUNCNAME[@]}" || \ |
|
246 |
die "$nested_func() must be used by $func()" |
|
247 |
} |
|
248 |
|
|
249 |
|
|
227 | 250 |
#### exceptions |
228 | 251 |
|
229 | 252 |
fi # load new aliases |
... | ... | |
612 | 635 |
} |
613 | 636 |
|
614 | 637 |
|
615 |
#### functions |
|
616 |
|
|
617 |
func_exists() { declare -f "$1" >/dev/null; } |
|
618 |
|
|
619 |
copy_func() # usage: from=... to=... copy_func |
|
620 |
# $to must not exist. to get around the no-clobber restriction, use `unset -f`. |
|
621 |
{ |
|
622 |
: "${from:?}" "${to:?}" |
|
623 |
func_exists "$from" || die "function does not exist: $from" |
|
624 |
! func_exists "$to" || die "function already exists: $to" |
|
625 |
local from_def="$(declare -f "$from")" |
|
626 |
eval "$to${from_def#$from}" |
|
627 |
} |
|
628 |
|
|
629 |
func_override() # usage: func_override old_name__suffix |
|
630 |
{ from="${1%__*}" to="$1" copy_func; } |
|
631 |
|
|
632 |
ensure_nested_func() # usage: func__nested_func() { ensure_nested_func; ... } |
|
633 |
{ |
|
634 |
local nested_func="${FUNCNAME[1]}" |
|
635 |
local func="${nested_func%%__*}" |
|
636 |
contains "$func" "${FUNCNAME[@]}" || \ |
|
637 |
die "$nested_func() must be used by $func()" |
|
638 |
} |
|
639 |
|
|
640 |
|
|
641 | 638 |
#### paths |
642 | 639 |
|
643 | 640 |
# cache realpath |
Also available in: Unified diff
lib/sh/util.sh: 2nd functions section: moved to 1st functions section