如何在 shell 脚本中将数组分配为值
How to assign array as value in shell script
我是 shell 脚本的新手,我正在尝试替换对象内的数组,但它不起作用。
我正在尝试使用动态数据负载发出 curl 请求。
case $name in
"test1")
VALUE='[{"test": "1"},{"test": "2"}]'
;;
"test2")
VALUE='[{"test": "1"},{"test": "3"}]'
;;
?)
echo "Error"
exit 1
;;
esac
我正在卷曲
$(curl -s -X POST \
"${BASE_URL}/pdc/config/add" \
-H "authorization: Bearer ${TOKEN}" \
-H "content-type: application/json" \
-d '{
"data": '\"${VALUE}\"'
}' \
--insecure
)
但这会将数组作为字符串发送。
要发送的数据应该是这样的
{"data": [{"test": "1"},{"test": "3"}]}
但现在发送的是
{"data": "[{"test": "1"},{"test": "3"}]"}
我该如何解决这个问题?
-d
选项应该是:
-d '{"data": '"${VALUE}"'}'
扩展为
-d {"data": [{"test": "1"},{"test": "3"}]}
对于shell,$VALUE
(或者等价地,${VALUE}
)是一个字符串。
我是 shell 脚本的新手,我正在尝试替换对象内的数组,但它不起作用。
我正在尝试使用动态数据负载发出 curl 请求。
case $name in
"test1")
VALUE='[{"test": "1"},{"test": "2"}]'
;;
"test2")
VALUE='[{"test": "1"},{"test": "3"}]'
;;
?)
echo "Error"
exit 1
;;
esac
我正在卷曲
$(curl -s -X POST \
"${BASE_URL}/pdc/config/add" \
-H "authorization: Bearer ${TOKEN}" \
-H "content-type: application/json" \
-d '{
"data": '\"${VALUE}\"'
}' \
--insecure
)
但这会将数组作为字符串发送。
要发送的数据应该是这样的
{"data": [{"test": "1"},{"test": "3"}]}
但现在发送的是
{"data": "[{"test": "1"},{"test": "3"}]"}
我该如何解决这个问题?
-d
选项应该是:
-d '{"data": '"${VALUE}"'}'
扩展为
-d {"data": [{"test": "1"},{"test": "3"}]}
对于shell,$VALUE
(或者等价地,${VALUE}
)是一个字符串。