json元素值,在shell脚本中用变量替换



我有一个带有主机列表的模板json文件作为示例。将其替换为shell脚本生成的动态值。

rds.json 示例

{
"lob": "coaching",
"function": "badminton",
"hosts": [
"node1.rds.sports.com",
"node2.rds.sports.com",
"node3.rds.sports.com"
],
"adminserver": "node1.rds.sports.com",
"user_name": "coach",
"sudo_type": "sudo",
"group_name": "admin"
}

echo $myHosts:

"host1.rds.sports.com", "host2.rds.sports.com", "host3.rds.sports.com", "host4.rds.sports.com", "host5.rds.sports.com", "host6.rds.sports.com", "host7.rds.sports.com", "host8.rds.sports.com"

$myHosts的值应在hosts元素键中替换。

期望输出:

{
"lob": "coaching",
"function": "badminton",
"hosts": [
"host1.rds.sports.com", 
"host2.rds.sports.com", 
"host3.rds.sports.com", 
"host4.rds.sports.com", 
"host5.rds.sports.com", 
"host6.rds.sports.com", 
"host7.rds.sports.com", 
"host8.rds.sports.com"
],
"adminserver": "hosts1.rds.sports.com",
"user_name": "coach",
"sudo_type": "sudo",
"group_name": "admin"
}

我假设myHosts的内容是一个有效的JSON数组体。

jq --argjson hosts "[$myHosts]" '.hosts = $hosts | .adminserver = .hosts[0]' rds.json

jqplay

相关内容

  • 没有找到相关文章

最新更新