aboutsummaryrefslogtreecommitdiff
path: root/shell_utils.sh
diff options
context:
space:
mode:
Diffstat (limited to 'shell_utils.sh')
-rw-r--r--shell_utils.sh28
1 files changed, 17 insertions, 11 deletions
diff --git a/shell_utils.sh b/shell_utils.sh
index 95e0d4e..6d4cc76 100644
--- a/shell_utils.sh
+++ b/shell_utils.sh
@@ -3,15 +3,8 @@
# This file is meant to be sourced in sh.
-ENDL="
-"
-
-errcho() {
- echo "$@" >&2
-}
-
map_set_instr() {
- echo "$1__$2='$3'"
+ printf "%s__%s='%s'" "$1" "$2" "$3"
}
map_set() {
@@ -23,11 +16,11 @@ map_set_export() {
}
map_get() {
- eval "echo \"\$$1__$2\""
+ eval "printf %s \"\$$1__$2\""
}
map_del_instr() {
- echo "unset $1__$2"
+ printf 'unset %s__%s' "$1" "$2"
}
map_del() {
@@ -35,5 +28,18 @@ map_del() {
}
sanitize() {
- echo "$1" | tr /.- _
+ printf %s "$1" | tr /.- _
+}
+
+escape_regex_special() {
+ printf %s "$1" | sed 's/\([]\.*?{},()[-]\)/\\\1/g'
+}
+
+# Note: We don't actually parse JSON. We extract needed keys with sed regexes
+# which does not work in the general case but is sufficient for now.
+get_json_key() {
+ local KEY_REG="$(escape_regex_special "$1")"
+ printf %s "$2" |
+ sed 's/\(.*"'"$KEY_REG"'"[[:space:]]*:[[:space:]]*"\([^"]*\)"\)\?.*/\2/' |
+ grep . | head -1
}