0

I am working on a script, where via JQ I am getting Environment variables and I need them in KV pairs for other type of file. Currently, i have the elements as follows in a Bash array

DD_TRACE_CLI_ENABLED:true
PRODUCT:buy_box
TRACE_ID:$NOMAD_ALLOC_ID

Now, When I am printing, I need it like this:

- name: DD_TRACE_CLI_ENABLED
  value: true
- name: PRODUCT
  value: buy_box

My current code

if [ ! -z "$env_params" -a "$env_params" != " " ]; then
env_params_as_array=(${env_params//,/ })
for each in "${env_params_as_array[@]}"
do
echo $each
echo -e "${myCustomIndentTab}- $each" >> values-$1.yaml
done
fi

How can I achieve that? Thank you.

We are Borg
  • 5,117
  • 17
  • 102
  • 225
  • It seems like you want to generate YAML. With the data coming from `jq` why don't you use `yq`? – Fravadona May 16 '22 at 10:07
  • If you are using `jq` anyway, probably use that tool to format the output the way you want it. See e.g. https://stackoverflow.com/a/68168384/874188 – tripleee May 16 '22 at 10:08

1 Answers1

2

bash array:

env_params_as_array=(
    DD_TRACE_CLI_ENABLED:true
    PRODUCT:buy_box
    TRACE_ID:"$NOMAD_ALLOC_ID"
)

printing it the way you want:

for p in "${env_params_as_array[@]}"
do
    IFS=':' read -r k v <<< "$p"
    printf -- '%s- name: %s\n  value: %s\n' "$myCustomIndentTab" "$k" "$v"
done > "values-$1.yaml"
Fravadona
  • 13,917
  • 1
  • 23
  • 35