【发布时间】:2018-11-08 20:43:07
【问题描述】:
我在创建然后使用值中包含双引号的变量时遇到了麻烦,并且真的很难找到任何其他具有此类示例的帖子,其中一些带有特殊字符,但没有专门关注使用双引号。
我希望在 bash 脚本中创建一个变量,它允许我执行以下 CURL 命令,并将“文本”值作为变量
curl -v -o POST \
-u "apikey:-------------------------------------" \
-H "Content-Type: application/json" \
-d '{
"text":"Hello World",
"features": {
"sentiment": {},
"categories": {},
"concepts": {},
"keywords": {},
"emotion": {}
}
}' \
"https://gateway-wdc.watsonplatform.net/natural-language-
understanding/api/v1/analyze?version=2018-03-19"
$SHELL
我已经尝试了以下方法,但是,虽然 echo 看起来传递了正确的值,但 CURL 响应是 400 错误 - 无效响应。
VAR1='"Hello World"'
echo "VAR1=${VAR1}"
echo
curl -v -o POST \
-u "apikey:-------------------------------------" \
-H "Content-Type: application/json" \
-d '{
"text":${VAR1},
"features": {
"sentiment": {},
"categories": {},
"concepts": {},
"keywords": {},
"emotion": {}
}
}' \
"https://gateway-wdc.watsonplatform.net/natural-language-
understanding/api/v1/analyze?version=2018-03-19"
$SHELL
【问题讨论】:
-
不要手动生成 JSON 数据。如果
$VAR1包含引号或反斜杠,它将失败。使用jq 安全可靠地执行此操作。