bash脚本中的卷曲命令问题,因为字符串变量包含引号 [英] Curl command issue in bash script because of string variable containing quotes

查看:70
本文介绍了bash脚本中的卷曲命令问题,因为字符串变量包含引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在bash脚本中执行curl命令。

I am executing curl command inside bash script.

问题在curl命令中,我必须传递一些变量($ summary,$ description)。
,但由于这些变量在单引号(')中,因此未分配变量。

Issue is in curl command I have to pass some variables($summary,$description). but as these variables are in single quote('), variables are not getting assigned.

仅供参考:我无法删除单引号,也可以用双引号代替。

FYI: I can't remove single quote and also can't replace with double quotes.

如何克服这种情况。

卷曲命令:

curl -X POST -d '{"summary": "$summary",  "description": "$description", "moduleMapAssets": [{"name":"Rates | IRD"},{"name":"CRD | CRD"}]}' -H "Content-Type: application/json"


推荐答案

您可以(并且必须)使用双引号,您只需要转义字符串中包含的双引号:

You can (and must) use double-quotes, you just need to escape the double-quotes that are part of the string:

curl -X POST -d "{\"summary\": \"$summary\",  \"description\": \"$description\", \"moduleMapAssets\": [{\"name\":\"Rates | IRD\"},{\"name\":\"CRD | CRD\"}]}" -H "Content-Type: application/json"

为@ MikeHolt在评论中指出,也可以在单个字符串中混合使用引号样式,因此您可以在包含文字双引号的单引号部分和包含变量引用的双引号部分之间来回切换:

As @MikeHolt pointed out in a comment, it's also possible to mix quoting styles within a single string, so you could switch back and forth between single-quoted sections that include literal double-quotes, and double-quoted sections that include variable references:

curl -X POST -d '{"summary": "'"$summary"'",  "description": "'"$description"'", "moduleMapAssets": [{"name":"Rates | IRD"},{"name":"CRD | CRD"}]}' -H "Content-Type: application/json"

要详细解释一下: ...'{ summary :' $ summary',描述 ...'被解析为单引号部分'{ summary:'(在双引号内是文字),紧接着是带双引号的段 $ summary (在其中将变量扩展)由另一个用单引号引起来的部分', descri ption ...'等。由于这些部分之间没有空格,因此将它们视为 curl 的一个长参数。

To explain that in a little more detail: ... '{"summary": "'"$summary"'", "description"...' is parsed as the single-quoted section '{"summary": "' (within which the double-quotes are literal), followed immediately by the double-quoted section "$summary" (within which the variable gets expanded), followed immediately by another single-quoted section '", "description"...' etc. Since there are no spaces between these sections, they're treated as one long argument to curl.

BTW,如果您的任何变量本身都可以包含双引号或反斜杠,则事情会变得更加复杂。如果可能出现这种情况,则应使用 jq 之类的东西来创建字符串。像这样的东西:

BTW, if any of your variables can contain double-quotes or backslashes themselves, things get much more complicated. If something like this is a possibility, you should be using something like jq to create the string. Something like this:

jsonstring=$(jq -n --arg summary "$summary" --arg description "$description" '{
    summary: $summary,
    description: $description,
    moduleMapAssets: [{name: "Rates | IRD"}, {name: "CRD | CRD"}]
    }' )
curl -X POST -d "$jsonstring" -H "Content-Type: application/json"

这篇关于bash脚本中的卷曲命令问题,因为字符串变量包含引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆