Home > database >  Pass bash variable in yq
Pass bash variable in yq

Time:10-05

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

CodePudding user response:

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/
  • Related