Say I have an example YAML file, example.yaml:
services:
test:
item0:
item1:
item2:
item3:
And I have the follow bash function to recurse through the YAML levels:
function readYAML {
local yaml="$1"
declare -a arr
readarray -t arr <<< "$(echo "$yaml" | yq -r '. |select(.) | keys | .[]' )"
echo "array -> ${arr[@]}"
for item in "${arr[@]}"; do
if [$item != ""]; then
echo $("'.$item'")
yq -r "$("'.$item'")" | readYAML;
fi
done
}
After running
readYAML "$(cat example.yaml)"
I get a yq error like '.test': command not found
How can I adjust the yq -r $("'.$item'") | readYAML;
line so that I end up with an array arr
that contains item0
... item3
?
I think that yq is interpreting $("'.$item'")
as a command not as a filtering instruction. So I am doing the variable substitution incorrectly somehow.
N.B.
i. I know arr
is overwritten (just stripped out array naming to simplify the question).
ii. I am using Go yq (Mike Farrah v4.31.2)
iii. There are similar questions. But none that I found dealing with substitute bash variables into a yq command within a bash function like this question: