Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to find match with yq in for loop

I have a yaml file called teams.yml that looks like this:

test:
  bonjour: hello
loop:
  bonjour: allo

I want to loop over an array and get itsvalues matching a key in the yaml. I have tried yq e 'keys | .[] | select(. == "test")' .github/teams.yml and it returns test whereas yq e 'keys | .[] | select(. == "abc")' .github/teams.yml returns nothing which is enough to get the information I am interested in.

The issue is that, when using the same logic in a for loop, yq returns nothing:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#!/bin/bash

yq e 'keys | .[] | select(. == "abc")' .github/teams.yml # Prints nothing
yq e 'keys | .[] | select(. == "test")' .github/teams.yml # Prints 'test'

ar=( "abc" "test" "xyz" )
for i in "${ar[@]}" # Prints nothing
do
  yq e 'keys | .[] | select(. == "$i")' .github/teams.yml 
done

What explains the lack of output in the for loop?

>Solution :

Call yq with the loop variable set in the environment, and use env within yq to read environment variables. See also the section Env Variable Operators in the manual.

ar=( "abc" "test" "xyz" )
for i in "${ar[@]}" # Prints nothing
do
  i="$i" yq e 'keys | .[] | select(. == env.i)' .github/teams.yml 
done
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading