需要帮助在数组内创建对象
jq 查询如下,
jq -n --arg cname "$1" --arg dns "$2" '
{
"extra_vars": {
"oper_tasks": [ "records" ],
"zone_info": [
{
"zone": "exmple.com",
"records": [
{
"name": $cname,
"type": "CNAME",
"value": $dns,
"ttl": 3600
}
]
}
]
}
}
'
上面的 jq cn 产生类似这样的内容,当我们作为脚本执行时,cname 和 dns 可以填充参数。
"extra_vars": {
"oper_tasks": [
"records"
],
"zone_info": [
{
"zone": "exmple.com",
"records": [
{
"name": "",
"type": "CNAME",
"value": "",
"ttl": 3600
}
]
}
]
}
}
我们如何从一个输入文件创建多个如下所示的对象。就像我们在 for 循环中所做的那样。
"extra_vars": {
"oper_tasks": [
"records"
],
"zone_info": [
{
"zone": "exmple.com",
"records": [
{
"name": <cname1 from input file>,
"type": "CNAME",
"value": "",
"ttl": 3600
},
{
"name": <cname2 from input file>,
"type": "CNAME",
"value": "",
"ttl": 3600
}
]
}
]
}
jq query as like this ,
jq -n --arg cname "$1" --arg dns "$2" '
{
"extra_vars": {
"oper_tasks": [ "records" ],
"zone_info": [
{
"zone": "exmple.com",
"records": [
{
"name": $cname,
"type": "CNAME",
"value": $dns,
"ttl": 3600
}
]
}
]
}
}
'
The above jq cn produce something like this , cname and dns can be filled with arguments when we execute as script.
"extra_vars": {
"oper_tasks": [
"records"
],
"zone_info": [
{
"zone": "exmple.com",
"records": [
{
"name": "",
"type": "CNAME",
"value": "",
"ttl": 3600
}
]
}
]
}
}
how we can create more than one objects like below from an input file. something like how we do in for loop.
"extra_vars": {
"oper_tasks": [
"records"
],
"zone_info": [
{
"zone": "exmple.com",
"records": [
{
"name": <cname1 from input file>,
"type": "CNAME",
"value": "",
"ttl": 3600
},
{
"name": <cname2 from input file>,
"type": "CNAME",
"value": "",
"ttl": 3600
}
]
}
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想象一下,您有一个文件
cnames.txt
,其中每一行都包含一个 CNAME 域,后跟其值:让我们创建该文件:
您可以将一个
jq
“嵌套”到其他可以使用jq 手册中描述的--slurpfile
。以下命令应该执行您想要的操作:它返回:
Imagine that you have a file
cnames.txt
where each line contains one CNAME domain followed by its value:Let us create that file:
You can "nest" one
jq
into the other by using--slurpfile
described in the jq Manual. The following command should do what you want:which returns: