-2

I want to use the value of a variable USER_PROXY in the JQ query statement.

export USER_PROXY= "proxy.zyz.com:122"

BY refering the SO answer no:1 from HERE , and also the LINK, I made the following shell script.

jq -r --arg UPROXY ${USER_PROXY} '.proxies = {
            "default": {
            "httpProxy": "http://$UPROXY\",
            "httpsProxy": "http://$UPROXY\",
            "noProxy": "127.0.0.1,localhost"
            }
            }'  ~/.docker/config.json > tmp && mv tmp ~/.docker/config.json

However, I see I get the bash error as below. What is it that is missing here. Why is JQ variable UPROXY not getting the value from USER_PROXY bash variable.

enter image description here

codeLover
  • 3,720
  • 10
  • 65
  • 121
  • Please remove the image and paste the error as text in your question. –  Jun 20 '21 at 17:53

1 Answers1

2
export USER_PROXY= "proxy.zyz.com:122"
  1. You can't have a space here. This sets USER_PROXY to an empty string and tries to export a non-existant variable 'proxy.zyz.com:122'. You probably want
export USER_PROXY="proxy.zyz.com:122"
jq -r --arg UPROXY ${USER_PROXY} '.proxies = {
            "default": {
            "httpProxy": "http://$UPROXY\",
            "httpsProxy": "http://$UPROXY\",
            "noProxy": "127.0.0.1,localhost"
            }
            }'  ~/.docker/config.json > tmp && mv tmp ~/.docker/config.json
  1. You need quotes around ${USER_PROXY} otherwise any whitespace in it will break it. Instead use --arg UPROXY "${USER_PROXY}".
  2. This isn't the syntax for using variables inside a string in jq. Instead of "...$UPROXY..." you need "...\($UPROXY)..."
  3. You are escaping the " at the end of the string by putting a \ before it. I am not sure what you mean here. I think you perhaps meant to use a forward slash instead?

This last issue is the immediate cause of the error message you're saying. It says "syntax error, unexpected IDENT, expecting '}' at line 4" and then shows you what it found on line 4: "httpsProxy": .... It parsed the string from line 3, which looks like: "http://$UPROXY\"\n " because the escaped double quote doesn't end the string. After finding the end of the string on line 4, jq expects to find a } to close the object, or a , for the next key-value-pair, but it finds httpsProxy, which looks like an identifier. So that's what the error message is saying. It found an IDENTifier when it was expecting a } (or a , but it doesn't mention that).

Weeble
  • 17,058
  • 3
  • 60
  • 75
  • 2
    Missed one: `--arg UPROXY ${USER_PROXY}` should be `--arg UPROXY "${USER_PROXY}"` (or just `--arg UPROXY "$USER_PROXY"`) – ikegami Jun 18 '21 at 17:21