-1

Below is the JSON file:

{
    "awsIAMCred" : "replaceme",
    "DatabaseName" : "replaceme"
}

Below is the bash file:

#!/bin/bash

echo BEGIN
date '+%Y-%m-%d %H:%M:%S'

TOKEN=mouse

touch connector.json

yum -y install moreutils            
yum -y install nano
yum -y install nc
echo install of nano,nc and moreutils are done
chmod 660 connector.json

jq '.awsIAMCred = "hello"' connector.json|sponge connector.json
jq '.DatabaseName = $TOKEN' connector.json|sponge connector.json
cat connector.json

I am getting this exception:

jq: error: TOKEN/0 is not defined at , line 1:

Please help.

peak
  • 105,803
  • 17
  • 152
  • 177
user3164488
  • 33
  • 1
  • 4

1 Answers1

1

As pointed out in a comment, the second invocation of jq in the question can be made to work, but using string interpolation is generally regarded as an anti-pattern that's best avoided. There are numerous alternatives, e.g.:

jq --arg token "$TOKEN" '.DatabaseName = $token' connector.json

Please also note that both updates can, and perhaps should, be performed with just one invocation of jq, e.g. along the lines of:

jq --arg token "$TOKEN" '.awsIAMCred = "hello" | .DatabaseName = $token' connector.json
peak
  • 105,803
  • 17
  • 152
  • 177