Move query_srv function to libipa.sh
[libutil.sh.git] / 02-util.sh
blob4ad0d59a464775d321fee8d3df020fad708ff03a
1 getv() {
2 eval 'echo -n "$'"$1"'"'
5 setv() {
6 eval "${1}='$(echo -n "$2" | sed "s/'/'\\\\''/g")'"
9 are_perms() {
10 [ "$(stat -c '%a' "$2")" = "$1" ]
13 # -m : print missing tools
14 has_tools() {
15 local missing
16 local show=false
17 for i in "$@"
19 if [ "$i" = '-m' ]; then
20 show=true
21 else
22 command -v "$i" >/dev/null || missing="${missing} ${i}"
24 done
25 "$show" && echo -n $missing
26 [ ! "$missing" ]
29 check_tools() {
30 local missing
31 missing="$(has_tools -m "$@")" || fatal 1 "tool(s) missing to run this command:" -h "$missing"
34 try() {
35 local output
36 output="$("$@")" \
37 && { info_p success; [ "$output" ] && info -- "$output"; true; } \
38 || { info_p failure; error -- "$output"; }
41 push() {
42 if [ $# -ge 2 ]; then
43 local var="$1"
44 shift
45 local val="$(getv "$var")"
46 if [ "$val" ]
47 then setv "$var" "${val} ${@}"
48 else setv "$var" "$@"
53 contain() {
54 local v="$1"
55 shift
56 for i in "$@"
58 [ "$i" = "$v" ] && return 0
59 done
60 false