forked from GambitBSM/gambit-test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyaml.bash
More file actions
35 lines (30 loc) · 890 Bytes
/
yaml.bash
File metadata and controls
35 lines (30 loc) · 890 Bytes
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
#!/bin/bash
#
# Read a yaml file in bash - https://gist.github.com/pkuczynski/8665367
#
# Written by A. Fowlie.
# Parse a yaml file into bash X = ... commands
parse_yaml() {
local yaml=$1
local s='[[:space:]]*'
local w='[a-zA-Z0-9_]*'
local fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$yaml" |
awk -F"$fs" '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s=\"%s\"\n", vn, $2, $3);
}
}' |
sed 's/#.*/"/' | # Remove comments
sed 's/[[:blank:]]//g' # Remove blank spaces
}
# Evaluate yaml file parsed in to X = ... commands
source_yaml() {
local yaml=$1
eval "$(parse_yaml "$yaml")"
}