将JSON直接存储在带有变量的bash脚本中吗? [英] Store JSON directly in bash script with variables?
问题描述
在我的开头,我说不,找到一种不同的方式来做这件事"在这里是可以接受的答案.
I'm going to preface by saying that "no, find a different way to do it" is an acceptable answer here.
是否存在一种可靠的方法来将少量JSON存储在bash变量中,以便在从同一脚本运行的AWS CLI命令中使用?
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?
我将在Jenkins上运行一项作业,该作业正在更新AWS Route53记录,这需要使用记录中的更改进行UPSERTing JSON文件.因为它是从Jenkins运行的,所以没有本地存储可以保存此文件,因此我非常想避免每次运行该项目时(每个小时一次)都需要进行git checkout.
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的设置,将数据存储在变量($foo
)中并作为change-resource-record-sets
命令的一部分进行调用将是最方便的,但是我不完全了解如何在内部引用/存储JSON. bash-能安全完成吗?
Ideally, storing the data in a variable ($foo
) and calling it as part of the change-resource-record-sets
command 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?
在这种情况下,以下是特定的JSON;
The specific JSON in this case is the following;
{"Comment":"Update DNSName.","Changes":[{"Action":"UPSERT","ResourceRecordSet":{"Name":"alex.","Type":"A","AliasTarget":{"HostedZoneId":"######","DNSName":"$bar","EvaluateTargetHealth":false}}}]}
作为一个复杂的问题,需要扩展DNSName值-$bar
.
As an added complication the DNSName value - $bar
- needs to be expanded.
推荐答案
您可以使用此处文档:
foo=$(cat <<EOF
{"Comment":"Update DNSName.","Changes":[{"Action":"UPSERT","ResourceRecordSet":{"Name":"alex.","Type":"A","AliasTarget":{"HostedZoneId":"######","DNSName":"$bar","EvaluateTargetHealth":false}}}]}
EOF
)
通过在第一行中保留EOF
的位置,here-doc的内容将进行参数扩展,因此您的$bar
会扩展为您在其中放置的内容.
By leaving EOF
in the first line unquoted, the contents of the here-doc will be subject to parameter expansion, so your $bar
expands to whatever you put in there.
如果JSON中可以有换行符,则可以使其更具可读性:
If you can have linebreaks in your JSON, you can make it a little more readable:
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
)
并显示其存储方式,包括引号(假设bar=baz
):
and to show how that is stored, including quoting (assuming that bar=baz
):
$ declare -p foo
declare -- foo="{
\"Comment\": \"Update DNSName.\",
\"Changes\": [
{
\"Action\": \"UPSERT\",
\"ResourceRecordSet\": {
\"Name\": \"alex.\",
\"Type\": \"A\",
\"AliasTarget\": {
\"HostedZoneId\": \"######\",
\"DNSName\": \"baz\",
\"EvaluateTargetHealth\": false
}
}
}
]
}"
因为这会扩展一些外壳元字符,如果您的JSON包含类似`
的内容,您可能会遇到麻烦,因此,也可以直接分配,但是要小心在$bar
周围加上引号:
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
:
foo='{"Comment":"Update DNSName.","Changes":[{"Action":"UPSERT","ResourceRecordSet":{"Name":"alex.","Type":"A","AliasTarget":{"HostedZoneId":"######","DNSName":"'"$bar"'","EvaluateTargetHealth":false}}}]}'
请注意$bar
的引号:是
"'"$bar"'"
│││ │││
│││ ││└ literal double quote
│││ │└ opening syntactical single quote
│││ └ closing syntactical double quote
││└ opening syntactical double quote
│└ closing syntactical single quote
└ literal double quote
这篇关于将JSON直接存储在带有变量的bash脚本中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!