summaryrefslogtreecommitdiffhomepage
path: root/subr/ex_rtl.subr
blob: 0ab30885d3647d354db6bb21ae17ce797f67faf9 (plain)
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
#
# set -o errexit -o noglob are assumed.
#

EXP_IFS_NL="
";

ex_basename() { echo "${1##*/}"; };
ex_date() { command date "+${1:-${TIMESTAMP_FMT}}"; };
ex_dirname() { echo "${1%/*}"; };
ex_get_var_unsafe() { eval echo \${${1}}; };
ex_set_var_unsafe() { [ -n "${2}" ] && eval ${1}=\"${2}\" || return 0; };
ex_pop_IFS() { IFS="${EXP_OLDIFS}"; };
ex_push_IFS() { EXP_OLDIFS="${IFS}"; IFS="${1}"; };
ex_push_IFS_nl() { ex_push_IFS "${EXP_IFS_NL}"; };
ex_test_cmd() { command -v "${1}" >/dev/null; };
ex_toupper() { echo "${1}" | tr a-z A-Z; };

ex_lfilter() {
	local _list="${1}" _filter="${2}" _lnew _litem _litem_filter;
	if [ -z "${_filter}" ]; then
		echo "${_list}"; return 0;
	else for _litem in ${_list}; do
		for _litem_filter in ${_filter}; do
			if [ "${_litem_filter}" = "${_litem}" ]; then
				_lnew="${_lnew:+${_lnew} }${_litem}";
				break;
			fi;
		done;
	done; fi;
	echo "${_lnew}";
};

ex_lfilter_not() {
	local _list="${1}" _filter="${2}" _lnew _litem _litem_filter _filterfl;
	if [ -z "${_filter}" ]; then
		echo "${_list}"; return 0;
	else for _litem in ${_list}; do
		_filterfl=0;
		for _litem_filter in ${_filter}; do
			if [ "${_litem_filter}" = "${_litem}" ]; then
				_filterfl=1; break;
			fi;
		done;
		if [ "${_filterfl:-0}" -eq 0 ]; then
			_lnew="${_lnew:+${_lnew} }${_litem}";
		fi;
	done; fi;
	echo "${_lnew}";
};

ex_lmatch() {
	local _cmp="${3}" IFS="${2}"; set -- ${1};
	while [ ${#} -gt 0 ]; do
		if [ "${1}" = "${_cmp}" ]; then
			return 0;
		fi; shift;
	done; return 1;
};

ex_run_cmd_unsplit() {
	local _cmd="${1}" _cmdline _rc IFS; shift;
	while [ ${#} -gt 0 ]; do
		[ -n "${1}" ] &&\
			_cmdline="${_cmdline:+${_cmdline}:}${1}";
		shift;
	done;
	IFS=:; ${_cmd} ${_cmdline}; _rc=$?;
	return ${_rc};
};

# vim:filetype=sh