0

i'm new to shelling in linux.

i'm trying to write a shell script that uses a REST method and creates some datasources on grafana

i need the url to be modular and to be taken from an argument. this is the line i'm trying to run:

srv_url = "1.1.1.1:8080"

RESULT=$(/bin/curl --user admin:admin 'http://localhost:3000/api/datasources' -X POST -H 'Content-Type: application/json;charset=UTF-8' --data-binary '{"name":"Events","isDefault":false ,"type":"influxdb","url":"http://$srv_url","access":"proxy","basicAuth":false}')

as you can see i'm trying to plant the variable $srv_url inside ("url":"http://$srv_url"), but it will not take its value, whatever i tried the script uses its literal name, and not its value.

any ideas?

thanks.

David Gidony
  • 1,243
  • 3
  • 16
  • 31

1 Answers1

1

Variable substitution in strings only works with double quotes, not single quotes. That means that you'll have to escape all of the quotes in the string or use single quotes:

srv_url = "1.1.1.1:8080"

RESULT=$(/bin/curl --user admin:admin 'http://localhost:3000/api/datasources' -X POST -H 'Content-Type: application/json;charset=UTF-8' --data-binary "{\"name\":\"Events\",\"isDefault\":false ,\"type\":\"influxdb\",\"url\":\"http://$srv_url\",\"access\":\"proxy\",\"basicAuth\":false}")

Alternatively, you could do something like this by ending the single quote string and wrapping the variable in double quotes:

RESULT=$(/bin/curl --user admin:admin 'http://localhost:3000/api/datasources' -X POST -H 'Content-Type: application/json;charset=UTF-8' --data-binary '{"name":"Events","isDefault":false ,"type":"influxdb","url":"http://'"$srv_url"'","access":"proxy","basicAuth":false}')

You can read more about strings and variable substitutions in Bash here.

Kevin Hoerr
  • 2,319
  • 1
  • 10
  • 21