【问题标题】:bash variable in curl commandcurl命令中的bash变量
【发布时间】:2014-01-17 03:13:19
【问题描述】:

在下面的示例中(我从 PagerDuty 网页获得):

machine="hi" curl -H "Content-type: application/json" -X POST \
-d "{ \"service_key\": \"e93facc04764012d7bfb002500d5d1a6\", \"description\": \"FAILURE for production/HTTP on machine $machine\" }" \
"https://events.pagerduty.com/generic/2010-04-15/create_event.json"

我想在描述中使用变量,例如:

  "description": "FAILURE for $machine",

但是它不起作用,它只显示"FAILURE for $machine", 我试过"FAILURE for ${machine}",,但它也不起作用。你知道怎么解决吗?

【问题讨论】:

  • 这个问题毫无意义。您不应编辑问题以消除对答案的需要。请恢复到版本 2;版本 3 中的编辑完全错误。

标签: bash variables curl


【解决方案1】:

问题是使用单引号。您需要在字符串中使用双引号和转义和双引号:

curl -H "Content-type: application/json" -X POST \
-d "{    
  \"service_key\": \"e93facc04764012d7bfb002500d5d1a6\",
  ...
  \"description\": \"FAILURE for production/HTTP on machine $machine\"
}" \
"https://events.pagerduty.com/generic/2010-04-15/create_event.json"

相当乏味,但它会完成这项工作。

【讨论】:

  • 谢谢 为什么要为服务密钥这样做?
  • 只是为了展示一个例子。如果对字符串使用双引号,则需要在文本中转义任何 "。
  • 它不起作用它显示我 {"status":"unrecognized object","message":"Event object format is unrecognized","errors":["JSON parse error"]}T
  • 抱歉,忘记转义其中一个引号。
  • 哦,那是我在记事本++中编辑的问题。非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-06
  • 1970-01-01
  • 2020-07-06
  • 2018-11-16
  • 1970-01-01
  • 1970-01-01
  • 2012-03-02
相关资源
最近更新 更多