Revision 6106c789
Added by koszko almost 2 years ago
| shell_utils.sh | ||
|---|---|---|
| 3 | 3 |
|
| 4 | 4 |
# This file is meant to be sourced in sh. |
| 5 | 5 |
|
| 6 |
ENDL=" |
|
| 7 |
" |
|
| 8 |
|
|
| 9 |
# A "raw" echo, interprets neither backclash escapes nor command-line options. |
|
| 10 |
# Does not emit trailing newline. |
|
| 11 |
ech() {
|
|
| 12 |
printf %s "$*" |
|
| 13 |
} |
|
| 14 |
|
|
| 15 |
errcho() {
|
|
| 16 |
echo "$@" >&2 |
|
| 17 |
} |
|
| 18 |
|
|
| 19 | 6 |
map_set_instr() {
|
| 20 |
echo "$1__$2='$3'"
|
|
| 7 |
printf "%s__%s='%s'" "$1" "$2" "$3"
|
|
| 21 | 8 |
} |
| 22 | 9 |
|
| 23 | 10 |
map_set() {
|
| ... | ... | |
| 29 | 16 |
} |
| 30 | 17 |
|
| 31 | 18 |
map_get() {
|
| 32 |
eval "echo \"\$$1__$2\""
|
|
| 19 |
eval "printf %s \"\$$1__$2\""
|
|
| 33 | 20 |
} |
| 34 | 21 |
|
| 35 | 22 |
map_del_instr() {
|
| 36 |
echo "unset $1__$2"
|
|
| 23 |
printf 'unset %s__%s' "$1" "$2"
|
|
| 37 | 24 |
} |
| 38 | 25 |
|
| 39 | 26 |
map_del() {
|
| ... | ... | |
| 41 | 28 |
} |
| 42 | 29 |
|
| 43 | 30 |
sanitize() {
|
| 44 |
echo "$1" | tr /.- _
|
|
| 31 |
printf %s "$1" | tr /.- _
|
|
| 45 | 32 |
} |
| 46 | 33 |
|
| 47 | 34 |
escape_regex_special() {
|
| 48 |
ech "$1" | sed 's/\([]\.*?{},()[-]\)/\\\1/g'
|
|
| 35 |
printf %s "$1" | sed 's/\([]\.*?{},()[-]\)/\\\1/g'
|
|
| 49 | 36 |
} |
| 50 | 37 |
|
| 51 | 38 |
# Note: We don't actually parse JSON. We extract needed keys with sed regexes |
| 52 | 39 |
# which does not work in the general case but is sufficient for now. |
| 53 | 40 |
get_json_key() {
|
| 54 | 41 |
local KEY_REG="$(escape_regex_special "$1")" |
| 55 |
ech "$2" |
|
|
| 42 |
printf %s "$2" |
|
|
| 56 | 43 |
sed 's/\(.*"'"$KEY_REG"'"[[:space:]]*:[[:space:]]*"\([^"]*\)"\)\?.*/\2/' | |
| 57 | 44 |
grep . | head -1 |
| 58 | 45 |
} |
Also available in: Unified diff
rewrite parts of build script in awk