users.I想用bash脚本运行一个curl命令。 (下面的命令在终端中完美工作)
curl -i -H "Content-Type: application/json" -X POST -d '{"mountpoint":"/gua-la-autentica-1426251559"}' http://127.0.0.1:5000/connect但无法在bash中运行此命令。 当在一个变量($ final)中给出安装点值时。
final="/gua-la-autentica-1426251559" curl -i -H "Content-Type: application/json" -X POST -d '{"mountpoint":'$final'}' http://127.0.0.1:5000/connect有人可以帮助我,如何在单引号内回显变量?
users.I want to run a curl command with a bash script. (below command works perfectly in terminal)
curl -i -H "Content-Type: application/json" -X POST -d '{"mountpoint":"/gua-la-autentica-1426251559"}' http://127.0.0.1:5000/connectBut unable to run this command in bash. When mountpoint value is given in a variable($final).
final="/gua-la-autentica-1426251559" curl -i -H "Content-Type: application/json" -X POST -d '{"mountpoint":'$final'}' http://127.0.0.1:5000/connectCould someone please help me, how to echo variable inside single quotes?
最满意答案
JSON字符串值应该引用,参数扩展也应该引用。 您可以通过在整个JSON字符串周围使用双引号并转义内部双引号来实现此目的,如下所示:
curl -i -H "Content-Type: application/json" -X POST -d "{\"mountpoint\":\"$final\"}" http://127.0.0.1:5000/connect正如评论中所提到的,更强大的方法是使用jq等工具生成JSON:
json=$(jq -n --arg final "$final" '{ mountpoint: $final }') curl -i -H "Content-Type: application/json" -X POST -d "$json" http://127.0.0.1:5000/connectJSON string values should be quoted and so should parameter expansions. You can achieve this by using double quotes around the entire JSON string and escaping the inner double quotes, like this:
curl -i -H "Content-Type: application/json" -X POST -d "{\"mountpoint\":\"$final\"}" http://127.0.0.1:5000/connectAs mentioned in the comments, a more robust approach would be to use a tool such as jq to generate the JSON:
json=$(jq -n --arg final "$final" '{ mountpoint: $final }') curl -i -H "Content-Type: application/json" -X POST -d "$json" http://127.0.0.1:5000/connect更多推荐
发布评论