forked from boochtek/mac_config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaps.sh
executable file
·40 lines (33 loc) · 813 Bytes
/
maps.sh
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
#!/bin/bash
## Helper functions for working with "maps" in Bash 3.
## The "maps" are arrays of strings, each string being a key, followed by '=', followed by the value.
# Given a map and a key, returns the value from the map.
function map_value_for_key {
local key_to_find="${@: -1}"
local map=("$@")
unset map[${#map[@]}-1]
for mapping in "${map[@]}" ; do
local key=${mapping%%=*}
local value=${mapping#*=}
if [ "$key_to_find" == "$key" ]; then
echo "$value"
return
fi
done
}
function map_keys {
local map=("$@")
local mapping
for mapping in "${map[@]}" ; do
local key=${mapping%%=*}
echo "$key"
done
}
function map_values {
local map=("$@")
local mapping
for mapping in "${map[@]}" ; do
local value=${mapping#*=}
echo "$value"
done
}