![](/img/trans.png)
[英]Bash - handling of (single) quotes in variables (for a Curl command)
[英]Passing variable into CURL command using bash scripting (with double quotes in variables value)
我在創建然后使用包含值中包含雙引號的變量時遇到了麻煩,並且確實很難找到帶有此類示例的任何其他帖子,其中一些具有特殊字符,但沒有一個專門考慮使用雙引號。
我正在尋找在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
我嘗試了以下操作,但是,盡管回聲看起來像是傳遞了正確的值,但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
(對此有一個重復項,但我永遠找不到。)
使用jq
類的工具來構建JSON。
data=$(jq --argjson x "$VAR1" '
{
text: $x,
features: {},
sentiment: {},
categories: {},
concepts: {},
keywords: {},
emotion: {}
}'
)
curl ... -d "$data" ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.