Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash Centos sh-在execute命令中植入变量_Bash_Shell_Rest_Sh_Centos7 - Fatal编程技术网

Bash Centos sh-在execute命令中植入变量

Bash Centos sh-在execute命令中植入变量,bash,shell,rest,sh,centos7,Bash,Shell,Rest,Sh,Centos7,我不熟悉linux中的shell 我正在尝试编写一个shell脚本,它使用REST方法并在grafana上创建一些数据源 我需要的url是模块化的,并采取从一个参数。 这是我正在尝试的路线: 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

我不熟悉linux中的shell

我正在尝试编写一个shell脚本,它使用REST方法并在grafana上创建一些数据源

我需要的url是模块化的,并采取从一个参数。 这是我正在尝试的路线:

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}')
如您所见,我试图将变量$srv_url植入url:http://$srv_url中,但它不会接受它的值,无论我尝试了什么,脚本都使用它的文字名称,而不是它的值

有什么想法吗


谢谢。

字符串中的变量替换只能使用双引号,不能使用单引号。这意味着您必须转义字符串中的所有引号或使用单引号:

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}")
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}')
或者,您可以通过结束单引号字符串并将变量用双引号括起来来执行类似操作:

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}")
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}')
您可以在Bash中阅读有关字符串和变量替换的更多信息