[英]how can i append a file into another file with a suitable format using script shell
I have this json file template.json我有这个 json 文件 template.json
{
"PTF_INSTALL_DATE": " 2020-03-31 09:12:10",
"PTF_CONTENT": [
{
"NAME": "api_batch_API",
"CHECKED": "api_batch_NOT_SELECTED",
"VERSION": "G02R00C13_c14"
}
]
}
And this file historique.json,It created by script bash , I call the script in Jenkinsfile而这个文件 historique.json,它是由脚本 bash 创建的,我在 Jenkinsfile 中调用该脚本
[
{
"PTF_INSTALL_DATE": " 2020-03-31 09:12:10",
"PTF_CONTENT": [
{
"NAME": "api_batch_API",
"CHECKED": "api_batch_NOT_SELECTED",
"VERSION": "G02R00C13_c14"
}
]
}
]
So when I do a build in jenkins an element has added in the last of file historique.json like below:因此,当我在 jenkins 中进行构建时,在 historique.json 文件的最后一个添加了一个元素,如下所示:
[
{
"PTF_INSTALL_DATE": " 2020-03-31 09:12:10",
"PTF_CONTENT": [
{
"NAME": "api_batch_API",
"CHECKED": "api_batch_NOT_SELECTED",
"VERSION": "G02R00C13_c14"
}
]
}
]
{
"PTF_INSTALL_DATE": " 2020-03-31 09:12:10",
"PTF_CONTENT": [
{
"NAME": "api_batch_API",
"CHECKED": "api_batch_NOT_SELECTED",
"VERSION": "G02R00C13_c14"
}
]
}
And ect.等等。 So my question is how can I formatted the historique file to be like below:所以我的问题是如何将历史文件格式化为如下所示:
[
{
"PTF_INSTALL_DATE": " 2020-03-31 09:12:10",
"PTF_CONTENT": [
{
"NAME": "api_batch_API",
"CHECKED": "api_batch_NOT_SELECTED",
"VERSION": "G02R00C13_c14"
}
]
},
{
"PTF_INSTALL_DATE": " 2020-03-31 09:12:10",
"PTF_CONTENT": [
{
"NAME": "api_batch_API",
"CHECKED": "api_batch_NOT_SELECTED",
"VERSION": "G02R00C13_c14"
}
]
}
]
This my script script.sh这是我的脚本 script.sh
#!/bin/bash
a=$(cat template.json)
if [ -e $WORKSPACE/project/historique.json ];
then
cat template.json >> historique.json
else
cat <<-EOF > historique.json
[
$a
]
EOF
fi
PS: The file template.json change at each build in jenkins PS:文件 template.json 在 jenkins 中每次构建时都会更改
The problem is the command after "then".问题是“then”之后的命令。 Any suggestion please??(And sorry for my English)请问有什么建议吗??(对不起我的英语)
After oguz ismail and shawn response the script file become like that:在 oguz ismail 和 shawn 响应之后,脚本文件变成这样:
#!/bin/bash
a=$(cat template.json)
if [ -e $WORKSPACE/project-CRMGP/historique.json ];
then
jq '. + [input]' historique.json template.json > historique1.json
else
cat <<-EOF > historique.json
[
$a
]
EOF
fi
mv historique1.json historique.json
This my script script.sh这是我的脚本 script.sh
Please use a decicated tool to process JSON.请使用专用工具来处理 JSON。 With xidel
for instance you don't even need the 'script.sh' at all:例如,使用xidel
,您甚至根本不需要“script.sh” :
$ xidel -s template.json -e '
if (file:exists("'$WORKSPACE/project-CRMGP/historique.json'")) then
array{
json-doc("'$WORKSPACE/project-CRMGP/historique.json'")(),
$json
}
else
array{$json}
'
[
{
"PTF_INSTALL_DATE": " 2020-03-31 09:12:10",
"PTF_CONTENT": [
{
"NAME": "api_batch_API",
"CHECKED": "api_batch_NOT_SELECTED",
"VERSION": "G02R00C13_c14"
}
]
},
{
"PTF_INSTALL_DATE": " 2020-03-31 09:12:10",
"PTF_CONTENT": [
{
"NAME": "api_batch_API",
"CHECKED": "api_batch_NOT_SELECTED",
"VERSION": "G02R00C13_c14"
}
]
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.