0

I'm trying to replace 1.2.3.4 with the contents of variable $wanip in the following script.

wanip="4.3.2.1"
echo $wanip
content=$(curl --insecure -H "X-DNSimple-Token: foo:bar" -H "Accept: application/json" -H "Content-Type: application/json" -X PUT -d "{\"record\": {\"name\": \"foo\",\"content\": \"1.2.3.4\"}}" https://acme.com/records/123)
echo $content

If I literally replace 1.2.3.4 with $wanip*, when I run the script I'm getting a message saying: "message":"Problems parsing JSON".

3
  • Works for me. Try using set -x before the command (and set +x after). It should print ++ curl --insecure -H 'X-DNSimple-Token: foo:bar' -H 'Accept: application/json' -H 'Content-Type: application/json' -X PUT -d '{"record": {"name": "foo","content": "4.3.2.1"}}' https://acme.com/records/123 -- if it prints something else, that may point to the problem. Commented Jan 16, 2015 at 4:44
  • put a set -x in the line before content? If so, that is giving me an illegal operation message Commented Jan 16, 2015 at 4:55
  • "illegal operation", or ": invalid option"? If the latter, your script may be in DOS/Windows format rather than unix, which will cause all sorts of weird problems. Try printing the script with cat -vt scriptname, and see if it lists "^M" at the end of lines. If it does, use something like dos2unix or sed -i $'s/\r$//' scriptname to fix it, and then stay away from Windows text editors for unix scripts. Commented Jan 16, 2015 at 5:36

2 Answers 2

1

Try adding a layer of abstraction:

#!/bin/bash
wnip="4.3.2.1"
echo $wanip
command="curl --insecure -H 'X-DNSimple-Token: foo:bar' -H 'Accept: application/json' -H 'Content-Type: application/json' -X PUT -d '{\"record\": {\"name\": \"foo\",\"content\": \"${wnip}\"}}' https://acme.com/records/123"
echo $command
content=$($command)
echo $content
Sign up to request clarification or add additional context in comments.

2 Comments

I'm getting several "couldn't resolve host" curl errors with this
@WhiskerBiscuit try this
0

After some hacking, I got this to work. Strange.

wanip=\"4.3.2.1\"
echo $wanip
content=$(curl --insecure -H "X-DNSimple-Token: foo:bar" -H "Accept: application/json" -H "Content-Type: application/json" -X PUT -d "{\"record\": {\"name\": \"foo\",\"content\": $wanip }}" https://acme.com/records/123)
echo $content

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.