bash 如何在 shell 脚本中的 curl 命令中传递变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13341955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass a variable in a curl command in shell scripting
提问by user1452759
I have a curl command:
我有一个 curl 命令:
curl -u ${USER_ID}:${PASSWORD} -X GET 'http://blah.gso.woo.com:8080/rest/job-execution/job-details/${job_id}'
The variable job_id
has a value in it, say, 1160. When I execute the curl command in shell it gives me the following error:
该变量中job_id
有一个值,例如 1160。当我在 shell 中执行 curl 命令时,它给了我以下错误:
{"message":"Sorry. An unexpected error occured.", "stacktrace":"Bad Request. The request could not be understood by the server due to malformed syntax."}
If I pass the number '1160' directly in the command, as shown below, the curl command works.
如果我直接在命令中传递数字“1160”,如下所示,curl 命令可以工作。
curl -u ${USER_ID}:${PASSWORD} -X GET 'http://blah.gso.woo.com:8080/rest/job-execution/job-details/1160'
I want to be able to pass the value of the variable in the curl command.
我希望能够在 curl 命令中传递变量的值。
回答by Gilles Quenot
When using variables in shell, you can only use doubles quotes, not single quotes : the variables inside single quotes are not expanded. Learn the difference between ' and " and `. See http://mywiki.wooledge.org/Quotesand http://wiki.bash-hackers.org/syntax/words
在shell 中使用变量时,只能使用双引号,不能使用单引号:单引号内的变量不展开。了解 ' 和 " 和 ` 之间的区别。请参阅http://mywiki.wooledge.org/Quotes和http://wiki.bash-hackers.org/syntax/words
回答by Robert Walters
I ran into this problem with passing as well, it was solved by using ' " $1 " '
我在通过时也遇到了这个问题,它是通过使用 '" $1 " ' 解决的
See connection.uribelow
请参阅下面的connection.uri
curl -X POST -H "Content-Type: application/json" --data '
{"name": "mysql-atlas-sink",
"config": {
"connector.class":"com.mongodb.kafka.connect.MongoSinkConnector",
"tasks.max":"1",
"topics":"mysqlstock.Stocks.StockData",
"connection.uri":"'""'",
"database":"Stocks",
"collection":"StockData",
"key.converter":"io.confluent.connect.avro.AvroConverter",
"key.converter.schema.registry.url":"http://schema-registry:8081",
"value.converter":"io.confluent.connect.avro.AvroConverter",
"value.converter.schema.registry.url":"http://schema-registry:8081",
"transforms": "ExtractField",
"transforms.ExtractField.type":"org.apache.kafka.connect.transforms.ExtractField$Value",
"transforms.ExtractField.field":"after"
}}' http://localhost:8083/connectors -w "\n"