如何在shell脚本中的curl命令中传递变量

时间:2012-11-12 10:22:08

标签: bash shell curl

我有一个curl命令:

curl -u ${USER_ID}:${PASSWORD} -X GET 'http://blah.gso.woo.com:8080/rest/job-execution/job-details/${job_id}'

变量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."}

如果我直接在命令中传递数字'1160',如下所示,curl命令有效。

curl -u ${USER_ID}:${PASSWORD} -X GET 'http://blah.gso.woo.com:8080/rest/job-execution/job-details/1160'

有人可以帮帮我吗?我想能够在curl命令中传递变量的值。

4 个答案:

答案 0 :(得分:44)

中使用变量时,您只能使用双引号,而不能使用单引号:单引号内的变量不会被展开。 了解'和'与`之间的区别。请参阅http://mywiki.wooledge.org/Quoteshttp://wiki.bash-hackers.org/syntax/words

答案 1 :(得分:12)

我也遇到了传递问题,通过使用'“ $ 1”'

请参见下面的 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":"'"$1"'",
     "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"

答案 2 :(得分:0)

userdetails="$username:$apppassword"
base_url_part='https://api.XXX.org/2.0/repositories'
path="/$teamName/$repoName/downloads/$filename"
base_url="$base_url_part$path"**strong text**
curl  -L -u "$userdetails" "$base_url" -o "$downloadfilename"

答案 3 :(得分:0)

如何使用 变量将 传递给

myvar=foobar
curl -H "Content-Type: application/json" --data @/dev/stdin<<EOF
{ "xkey": "$myvar" }
EOF

使用开关 -d--dataPOST 请求是隐式