Home > Software design >  assigning jq result to makefile variable
assigning jq result to makefile variable

Time:06-26

I have the following command as a makefile command:

update-env:
    echo "{ \"Variables\":${ENV_VALUE}}" > ./new_env.json && \
    UPDATE_ENVVARS=$$(jq -s '.[0] as $$a |.[1] as $$b | $$a * $$b' old_env.json new_env.json | jq -c . ) && \
    echo "${UPDATE_ENVVARS}"
  1. the ${ENV_VALUE} is taken when
make update-env ENV_VALUE="{\\\"HOST_URL\\\": \\\"https:\\\/\\\/test.com\\\"}"
  1. the file new_env.json is generated properly

  2. when executing jq -s '.[0] as $a |.[1] as $b | $a * $b' old_env.json new_env.json | jq -c . it generates the appropriate compact json result desired.

When running everything in sequence (the assignment and echo for validation), I get an empty result.

My goal for the command is to merge two json output and assign it to the UPDATE_ENVVARS for it to be reused as an input for another command that will accept the json. Per testing, it came out empty when echo, when I execute the jq solo, the merge output is functional.

CodePudding user response:

Only a minor bit of editing was needed:

update-env:
    echo '{ "Variables":${ENV_VALUE}}' > ./new_env.json && \
    UPDATE_ENVVARS=$$(jq -s '.[0] as $$a |.[1] as $$b | $$a * $$b' old_env.json new_env.json | jq -c . ) && \
    echo "$${UPDATE_ENVVARS}"

Note:

  • We're using single quotes in the first-line echo -- the substitution is performed by make, not by the shell, so single quotes don't suppress it.
  • We're doubling up the $$ in the last line, so we're expanding the shell variable set in the second line of the recipe, not a make variable that nothing ever set at all.

See this working at https://replit.com/@CharlesDuffy2/RoyalIdolizedProfile

By contrast, if you want to assign to a make variable instead of a shell variable, this question is duplicative of Makefile command substitution problem, and the answer by Ignacio Vazquez-Abrams is appropriate.

  • Related