将JSON直接存储在带有变量的bash脚本中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43373176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Store JSON directly in bash script with variables?
提问by Alex
I'm going to preface by saying that "no, find a different way to do it" is an acceptable answer here.
我要先说“不,找到一种不同的方法来做到这一点”在这里是一个可以接受的答案。
Is there a reliable way to store a short bit of JSON in a bash variable for use in a AWS CLI command running from the same script?
是否有一种可靠的方法可以将一小段 JSON 存储在 bash 变量中,以便在从同一脚本运行的 AWS CLI 命令中使用?
I'll be running a job from Jenkins that's updating an AWS Route53 record, which requires UPSERTing a JSON file with the change in records. Because it's running from Jenkins, there's no local storage where I can keep this file, and I'd really like to avoid needing to do a git checkout every time this project will run (which will be once an hour).
我将运行来自 Jenkins 的更新 AWS Route53 记录的作业,这需要 UPSERT 使用记录中的更改对 JSON 文件进行更新。因为它是从 Jenkins 运行的,所以没有本地存储可以保存这个文件,而且我真的很想避免每次这个项目运行时都需要做一次 git checkout(这将是一个小时一次)。
Ideally, storing the data in a variable ($foo) and calling it as part of the change-resource-record-setscommand would be most convenient given the Jenkins setup, but I'm unfamiliar with exactly how to quote/store JSON inside bash - can it be done safely?
理想情况下,鉴于 Jenkins 设置,将数据存储在变量 ( $foo) 中并将其作为change-resource-record-sets命令的一部分调用将是最方便的,但我不熟悉如何在 bash 中引用/存储 JSON - 可以安全地完成吗?
The specific JSON in this case is the following;
本例中具体的 JSON 如下;
{"Comment":"Update DNSName.","Changes":[{"Action":"UPSERT","ResourceRecordSet":{"Name":"alex.","Type":"A","AliasTarget":{"HostedZoneId":"######","DNSName":"$bar","EvaluateTargetHealth":false}}}]}
As an added complication the DNSName value - $bar- needs to be expanded.
作为增加的复杂性,DNSName 值 - $bar- 需要扩展。
回答by Benjamin W.
You could use a here-doc:
你可以使用here-doc:
foo=$(cat <<EOF
{"Comment":"Update DNSName.","Changes":[{"Action":"UPSERT","ResourceRecordSet":{"Name":"alex.","Type":"A","AliasTarget":{"HostedZoneId":"######","DNSName":"$bar","EvaluateTargetHealth":false}}}]}
EOF
)
By leaving EOFin the first line unquoted, the contents of the here-doc will be subject to parameter expansion, so your $barexpands to whatever you put in there.
通过EOF在第一行不加引号,here-doc 的内容将受到参数扩展的影响,因此您$bar将扩展为您在那里放置的任何内容。
If you can have linebreaks in your JSON, you can make it a little more readable:
如果您可以在 JSON 中使用换行符,则可以使其更具可读性:
foo=$(cat <<EOF
{
"Comment": "Update DNSName.",
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "alex.",
"Type": "A",
"AliasTarget": {
"HostedZoneId": "######",
"DNSName": "$bar",
"EvaluateTargetHealth": false
}
}
}
]
}
EOF
)
or even (first indent on each line must be a tab)
甚至(每行的第一个缩进必须是制表符)
foo=$(cat <<-EOF
{
"Comment": "Update DNSName.",
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "alex.",
"Type": "A",
"AliasTarget": {
"HostedZoneId": "######",
"DNSName": "$bar",
"EvaluateTargetHealth": false
}
}
}
]
}
EOF
)
and to show how that is stored, including quoting (assuming that bar=baz):
并显示它是如何存储的,包括引用(假设bar=baz):
$ declare -p foo
declare -- foo="{
\"Comment\": \"Update DNSName.\",
\"Changes\": [
{
\"Action\": \"UPSERT\",
\"ResourceRecordSet\": {
\"Name\": \"alex.\",
\"Type\": \"A\",
\"AliasTarget\": {
\"HostedZoneId\": \"######\",
\"DNSName\": \"baz\",
\"EvaluateTargetHealth\": false
}
}
}
]
}"
Because this expands some shell metacharacters, you could run into trouble if your JSON contains something like `, so alternatively, you could assign directly, but be careful about quoting around $bar:
因为这会扩展一些 shell 元字符,如果您的 JSON 包含类似的内容,您可能会遇到麻烦`,所以或者,您可以直接分配,但要小心引用$bar:
foo='{"Comment":"Update DNSName.","Changes":[{"Action":"UPSERT","ResourceRecordSet":{"Name":"alex.","Type":"A","AliasTarget":{"HostedZoneId":"######","DNSName":"'"$bar"'","EvaluateTargetHealth":false}}}]}'
Notice the quoting for $bar: it's
注意引用$bar:它是
"'"$bar"'"
│││ │││
│││ ││└ literal double quote
│││ │└ opening syntactical single quote
│││ └ closing syntactical double quote
││└ opening syntactical double quote
│└ closing syntactical single quote
└ literal double quote
回答by chepner
It can be storedsafely; generatingit is a different matter, since the contents of $barmay need to be encoded. Let a tool like jqhandle creating the JSON.
可以安全存放;生成它是另一回事,因为$bar可能需要对的内容进行编码。让像jq处理创建 JSON这样的工具。
var=$(jq -n --arg b "$bar" '{
Comment: "Update DNSName.",
Changes: [
{
Action: "UPSERT",
ResourceRecordSet: {
Name: "alex.",
Type: "A",
AliasTarget: {
HostedZoneId: "######",
DNSName: $b,
EvaluateTargetHealth: false
}
}
}
]
}')

