Project

General

Profile

Download (924 Bytes) Statistics
| Branch: | Tag: | Revision:

haketilo / shell_utils.sh @ 6106c789

1
# Copyright (C) 2021 Wojtek Kosior
2
# Redistribution terms are gathered in the `copyright' file.
3

    
4
# This file is meant to be sourced in sh.
5

    
6
map_set_instr() {
7
    printf "%s__%s='%s'" "$1" "$2" "$3"
8
}
9

    
10
map_set() {
11
    eval "$(map_set_instr "$@")"
12
}
13

    
14
map_set_export() {
15
    eval "export $(map_set_instr "$@")"
16
}
17

    
18
map_get() {
19
    eval "printf %s \"\$$1__$2\""
20
}
21

    
22
map_del_instr() {
23
    printf 'unset %s__%s' "$1" "$2"
24
}
25

    
26
map_del() {
27
    eval "$(map_del_instr "$@")"
28
}
29

    
30
sanitize() {
31
    printf %s "$1" | tr /.- _
32
}
33

    
34
escape_regex_special() {
35
    printf %s "$1" | sed 's/\([]\.*?{},()[-]\)/\\\1/g'
36
}
37

    
38
# Note: We don't actually parse JSON. We extract needed keys with sed regexes
39
# which does not work in the general case but is sufficient for now.
40
get_json_key() {
41
    local KEY_REG="$(escape_regex_special "$1")"
42
    printf %s "$2" |
43
	sed 's/\(.*"'"$KEY_REG"'"[[:space:]]*:[[:space:]]*"\([^"]*\)"\)\?.*/\2/' |
44
	grep . | head -1
45
}
(14-14/16)