WhiskerBiscuit
WhiskerBiscuit

Reputation: 5157

Using a variable in a script calling curl

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".

Upvotes: 0

Views: 82

Answers (2)

Nir Alfasi
Nir Alfasi

Reputation: 53565

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

Upvotes: 1

WhiskerBiscuit
WhiskerBiscuit

Reputation: 5157

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

Upvotes: 0

Related Questions