|
Lucio Andrés Illanes Albornoz |
c3bcd7 |
#
|
|
|
8d7a8a |
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 LucĂa Andrea Illanes Albornoz <lucia@luciaillanes.de>
|
|
Lucio Andrés Illanes Albornoz |
e1d469 |
# set +o errexit -o noglob -o nounset is assumed.
|
|
Lucio Andrés Illanes Albornoz |
c3bcd7 |
#
|
|
Lucio Andrés Illanes Albornoz |
c3bcd7 |
|
|
|
99f4d8 |
#
|
|
|
550c18 |
# rtl_export_vars() - export or unset list of variables
|
|
|
550c18 |
# @[-u]: unset instead of exporting variables
|
|
|
550c18 |
# @...: list of variable name-value pairs
|
|
|
550c18 |
#
|
|
|
99f4d8 |
# Returns: zero (0) on success, non-zero (>0) on failure
|
|
|
99f4d8 |
#
|
|
Lucio Andrés Illanes Albornoz |
c6d6e0 |
rtl_export_vars() {
|
|
|
e9fa07 |
local _rev_unsetfl=0;
|
|
|
e9fa07 |
|
|
|
e9fa07 |
if [ "x${1}" = "x-u" ]; then
|
|
|
e9fa07 |
_rev_unsetfl=1; shift;
|
|
|
e9fa07 |
fi;
|
|
Lucio Andrés Illanes Albornoz |
b9a715 |
|
|
Lucio Andrés Illanes Albornoz |
2b85d0 |
while [ "${#}" -ge 2 ]; do
|
|
Lucio Andrés Illanes Albornoz |
b9a715 |
if [ "${2:+1}" = 1 ]\
|
|
|
e9fa07 |
|| [ "${_rev_unsetfl}" -eq 1 ]; then
|
|
|
e9fa07 |
case "${_rev_unsetfl}" in
|
|
|
e9fa07 |
0) rtl_set_var_unsafe "${1}" "${2}";
|
|
|
e9fa07 |
export "${1}"; ;;
|
|
Lucio Andrés Illanes Albornoz |
b9a715 |
1) unset "${1}"; ;;
|
|
Lucio Andrés Illanes Albornoz |
b9a715 |
esac;
|
|
Lucio Andrés Illanes Albornoz |
b9a715 |
fi; shift 2;
|
|
Lucio Andrés Illanes Albornoz |
2b85d0 |
done;
|
|
Lucio Andrés Illanes Albornoz |
c3bcd7 |
|
|
|
e9fa07 |
return 0;
|
|
Lucio Andrés Illanes Albornoz |
8a27f9 |
};
|
|
Lucio Andrés Illanes Albornoz |
8a27f9 |
|
|
|
550c18 |
#
|
|
|
550c18 |
# rtl_percentage() - calculate percentage of two numbers
|
|
|
550c18 |
# @_in: input number
|
|
|
550c18 |
# @_max: input maximum
|
|
|
550c18 |
# @_rs_out: out reference to percentage number
|
|
|
550c18 |
#
|
|
|
550c18 |
# Returns: zero (0) on success, non-zero (>0) on failure
|
|
|
550c18 |
#
|
|
Lucio Andrés Illanes Albornoz |
285301 |
rtl_percentage() {
|
|
|
e9fa07 |
local _rp_in="${1}" _rp_max="${2}" _rp_rs_out="${3#\$}" \
|
|
|
e9fa07 |
_rp_perc;
|
|
|
e9fa07 |
|
|
|
e9fa07 |
_rp_perc=$((100 * ${_rp_in} + ${_rp_max} / 2));
|
|
|
e9fa07 |
_rp_perc=$((${_rp_perc} ? ${_rp_perc} / ${_rp_max} : ${_rp_perc}));
|
|
|
e9fa07 |
|
|
|
e9fa07 |
eval ${_rp_rs_out}='${_rp_perc}';
|
|
|
e9fa07 |
return 0;
|
|
|
e9fa07 |
};
|
|
|
e9fa07 |
|
|
|
550c18 |
#
|
|
|
550c18 |
# rtl_percentage2() - calculate percentage of two numbers
|
|
|
550c18 |
# @_rin: in reference to input number
|
|
|
550c18 |
# @_rmax: in reference to input maximum
|
|
|
550c18 |
# @_rs_out: out reference to percentage number
|
|
|
550c18 |
#
|
|
|
550c18 |
# Returns: zero (0) on success, non-zero (>0) on failure
|
|
|
550c18 |
#
|
|
|
e9fa07 |
rtl_percentage2() {
|
|
|
e9fa07 |
local _rp_rin="${1#\$}" _rp_rmax="${2#\$}" _rp_rs_out="${3#\$}" \
|
|
|
e9fa07 |
_rp_in=0 _rp_max=0 _rp_perc;
|
|
|
e9fa07 |
|
|
|
e9fa07 |
eval _rp_in=\"\${${_rp_rin}}\";
|
|
|
e9fa07 |
eval _rp_max=\"\${${_rp_rmax}}\";
|
|
|
e9fa07 |
|
|
|
e9fa07 |
_rp_perc=$((100 * ${_rp_in} + ${_rp_max} / 2));
|
|
|
e9fa07 |
_rp_perc=$((${_rp_perc} ? ${_rp_perc} / ${_rp_max} : ${_rp_perc}));
|
|
|
e9fa07 |
|
|
|
e9fa07 |
eval ${_rp_rs_out}='${_rp_perc}';
|
|
|
e9fa07 |
return 0;
|
|
Lucio Andrés Illanes Albornoz |
285301 |
};
|
|
Lucio Andrés Illanes Albornoz |
285301 |
|
|
|
550c18 |
#
|
|
|
550c18 |
# rtl_sunset() - unset variables
|
|
|
550c18 |
# @_rset: in reference to list of variables to unset
|
|
|
550c18 |
#
|
|
|
550c18 |
# Returns: zero (0) on success, non-zero (>0) on failure
|
|
|
550c18 |
#
|
|
Lucio Andrés Illanes Albornoz |
3e5303 |
rtl_sunset() {
|
|
|
550c18 |
local _rs_rset="${1#\$}" \
|
|
|
550c18 |
IFS=" ";
|
|
|
e9fa07 |
|
|
|
e9fa07 |
eval set -- "\${${_rs_rset}}";
|
|
Lucio Andrés Illanes Albornoz |
3e5303 |
while [ "${#}" -gt 0 ]; do
|
|
|
550c18 |
unset "${_rs_rset}"; shift;
|
|
Lucio Andrés Illanes Albornoz |
3e5303 |
done; unset "${_rs_rset}";
|
|
|
e9fa07 |
|
|
|
e9fa07 |
return 0;
|
|
Lucio Andrés Illanes Albornoz |
3e5303 |
};
|
|
Lucio Andrés Illanes Albornoz |
3e5303 |
|
|
|
64844b |
# vim:filetype=sh textwidth=0
|