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

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_split() { local IFS="${1}"; set -- $(echo "${2}"); IFS="${IFS_NL}"; echo "${*}"; };
ex_test_cmd() { command -v "${1}" >/dev/null; };
ex_toupper() { echo "${1}" | tr a-z A-Z; };

ex_lfilter() {
	[ "x${1}" = "x-not" ] && { local _notfl=1; shift; }
	local _list="${1}" _filter="${2}" _lnew _litem _ex_lfilter _filterfl;
	[ -z "${_filter}" ] && { echo "${_list}"; return 0; };
	for _litem in ${_list}; do
		_filterfl=0;
		for _ex_lfilter in ${_filter}; do
			if [ "${_notfl:-0}" -eq 0 ]\
			&& [ "${_ex_lfilter}" = "${_litem}" ]; then
				_lnew="${_lnew:+${_lnew} }${_litem}"; break;
			elif [ "${_notfl:-0}" -eq 1 ]\
			&& [ "${_ex_lfilter}" = "${_litem}" ]; then
				_filterfl=1; break;
			fi;
		done;
		if [ "${_notfl:-0}" -eq 1 ]\
		&& [ "${_filterfl:-0}" -eq 0 ]; then
			_lnew="${_lnew:+${_lnew} }${_litem}";
		fi;
	done;
	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