2

I am trying to pass bash variable in yq

test.yml

configuration:
 Properties:
  corporate-url: https://stackoverflow.com/

temp = '.configuration.Properties.corporate-url'
export $temp
Value1=$(yq '.[env($temp)]' test.yml)

expected output:

https://stackoverflow.com/

but I am getting this error(Actual output)

Error: Value for env variable '$variable1' not provided in env()

Please note: I am trying to fetch corporate-url value, using a bash variable, constraint is that I cannot pass string directly in yq as the value of temp changes as this snippet is running inside a for loop which changes value of temp every time so cannot hard code for a particular value.

Reference YQ Documentation: https://mikefarah.gitbook.io/yq/operators/env-variable-operators

ApisDraft folder contains multiple yml files

ApisDraft=$(find drafts/* -maxdepth 1 -type f)
for ApiFixOrgsTags in $ApisDraft
do
  my_var=$(yq '.securityDefinitions.[].tokenUrl' $ApiFixOrgsTags)
  ConfigProper='.configuration.Properties.'
  CatelogProper='.configuration.catalogs.[].Properties.'
  variable1=$ConfigProper${my_var}
  variable2=$CatelogProper${my_var}
  # to remove white all spaces
  variable1= echo $variable1 | sed -E 's/(\.) */\1/g'
  variable2= echo $variable2 | sed -E 's/(\.) */\1/g'
  export $variable1
  export $variable2
  Value1=$(yq "$variable1" $ApiFixOrgsTags)
  Value2=$(yq '.[env($variable2)]' $ApiFixOrgsTags)
done
Dhaval Patel
  • 21
  • 1
  • 6
  • sooooo why not just `yq "$temp"`? – KamilCuk Oct 03 '22 at 18:30
  • it will result in error – Dhaval Patel Oct 03 '22 at 18:39
  • Your yaml file is broken, it misses a space between `corporate-url:` and `https://stackoverflow.com/`. Then it will work using `temp='.configuration.Properties.corporate-url'; Value1="$(yq "$temp" test.yml)"` – pmf Oct 03 '22 at 18:39
  • Please consider it is ok, this is just for explanation purposes, the aim is to find logic – Dhaval Patel Oct 03 '22 at 18:44
  • I made an attempt to reformat this question. If I split anything up incorrectly, please correct. Note that if you put 4 spaces in front of a line in stackoverflow it will format it as code. – JNevill Oct 03 '22 at 18:47
  • Thank you Jnevil for formatting the question, it looks better – Dhaval Patel Oct 03 '22 at 18:50
  • 1
    I agree with @pmf. `Value1=$(yq "$temp" test.yml)` works just fine for me. What error are you getting, or why doesn't that work for your needs? – JNevill Oct 03 '22 at 18:54
  • Is the second snippet supposed to be bash code? If so, it contains several syntax errors: don't put spaces around `=` in an assignment, and don't use `$` when exporting variables. [shellcheck.net](https://www.shellcheck.net) is good at spotting mistakes like these. Also, they should've given you error messages as the script ran; did you get errors? – Gordon Davisson Oct 03 '22 at 18:57
  • Error: Bad expression, please check expression syntax – Dhaval Patel Oct 03 '22 at 19:03
  • I have added the complete code, I don't want to confuse people, please let me know if you have other questions – Dhaval Patel Oct 03 '22 at 19:09
  • No, Gordon just got error for the code: Value1=$(yq "$variable1" $ApiFixOrgsTags) – Dhaval Patel Oct 03 '22 at 19:12
  • Note that `$ApiFixOrgsTags` should itself be in quotes. – Charles Duffy Oct 03 '22 at 19:16
  • Also, `listOfFiles=$(find ...)` is itself buggy. See [BashPitfalls #1](https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29); for the better practice alternative, see [Using Find](https://mywiki.wooledge.org/UsingFind). – Charles Duffy Oct 03 '22 at 19:17
  • 1
    Also, `variable1= echo $variable1 | sed -E 's/(\.) */\1/g'` is not an assignment to `variable1`. See [How do I set a variable to the output of a command in bash?](https://stackoverflow.com/questions/4651437/how-do-i-set-a-variable-to-the-output-of-a-command-in-bash) – Charles Duffy Oct 03 '22 at 19:18

1 Answers1

1

In this case, you don't need to put it in the environment. Let the shell expand it so yq just sees the value of the variable:

yq "$temp" test.yml     # => https://stackoverflow.com/
glenn jackman
  • 238,783
  • 38
  • 220
  • 352