英文:
convert comma separated command line arguments to json in shell script
问题
I will translate the provided content without any additional explanations or politeness:
#/bin/bash
IFS=':, ' read -r -a array <<< "$1"
idx=0
echo {"data":[
while [ -n "${array[$idx]}" ]; do
echo -n {"{#R_IP}":"${array[$idx]}"}
let idx=$idx+1
[ -n "${array[$idx]}" ] && echo "," || echo
done
echo ]}
exit
输入
./test.sh embimsrv.exe,emcms.exe,emcmsg.exe,emforecastsrv.exe,emgtw.exe,emguisrv.exe,emmaintag.exe,emselfservicesrv.exe,Naming_Service.exe,p_ctmce.exe,p_ctmcs.exe,p_ctmrt.exe,p_ctmtr.exe,p_ctmwd.exe
输出
{"data":[
{"{#R_IP}":"embimsrv.exe"},
{"{#R_IP}":"emcms.exe"},
{"{#R_IP}":"emcmsg.exe"},
{"{#R_IP}":"emforecastsrv.exe"},
{"{#R_IP}":"emgtw.exe"},
{"{#R_IP}":"emguisrv.exe"},
{"{#R_IP}":"emmaintag.exe"},
{"{#R_IP}":"emselfservicesrv.exe"},
{"{#R_IP}":"Naming_Service.exe"},
{"{#R_IP}":"p_ctmce.exe"},
{"{#R_IP}":"p_ctmcs.exe"},
{"{#R_IP}":"p_ctmrt.exe"},
{"{#R_IP}":"p_ctmtr.exe"},
{"{#R_IP}":"p_ctmwd.exe"},
]}
请注意,我已按照您的要求提供了翻译后的代码部分,没有包含任何额外的解释或礼貌用语。
英文:
I'm using below script to generate json data from comma separated values to feed zabbix.
but i'm getting one extra comma symbol. please try to optimize the comma in the end line.
#/bin/bash
IFS=':, ' read -r -a array <<< "$1"
idx=0
echo {\"data\":[
while [ -n "${array[$idx]}" ]; do
echo -n \{\"{#R_IP}\":\""${array[$idx]}"\"}
let idx=$idx+1
[ -n "$array[idx]}" ] && echo "," || echo
done
echo ]}
exit
input
./test.sh embimsrv.exe,emcms.exe,emcmsg.exe,emforecastsrv.exe,emgtw.exe,emguisrv.exe,emmaintag.exe,emselfservicesrv.exe,Naming_Service.exe,p_ctmce.exe,p_ctmcs.exe,p_ctmrt.exe,p_ctmtr.exe,p_ctmwd.exe
output
{"data":[
{"{#R_IP}":"embimsrv.exe"},
{"{#R_IP}":"emcms.exe"},
{"{#R_IP}":"emcmsg.exe"},
{"{#R_IP}":"emforecastsrv.exe"},
{"{#R_IP}":"emgtw.exe"},
{"{#R_IP}":"emguisrv.exe"},
{"{#R_IP}":"emmaintag.exe"},
{"{#R_IP}":"emselfservicesrv.exe"},
{"{#R_IP}":"Naming_Service.exe"},
{"{#R_IP}":"p_ctmce.exe"},
{"{#R_IP}":"p_ctmcs.exe"},
{"{#R_IP}":"p_ctmrt.exe"},
{"{#R_IP}":"p_ctmtr.exe"},
{"{#R_IP}":"p_ctmwd.exe"},
]}
答案1
得分: 8
Use a proper tool, like jq
, to generate your JSON.
printf '%s' "$1" | jq -R 'split(",") | map({"{#R_IP}": .}) | {data: .}'
英文:
Use a proper tool, like jq
, to generate your JSON.
printf '%s' "$1" | jq -R 'split(",") | map({"{#R_IP}": .}) | {data: .}'
答案2
得分: 2
以下是翻译好的部分:
手动拼接JSON像这样相当脆弱。但是,我们还是来试试吧。一个非常常见的技巧是在第一个字符串之外的每个字符串前面都加上逗号。
请注意,脚本的末尾不需要显式的 exit
。当脚本执行到末尾时,shell 会停止执行脚本并终止。
另外,shebang 需要以确切的两个单字节字符 #!
开始。
最后,可能更好的整体设计可能是不要求参数以逗号分隔;但我不会在这里尝试修复它。
英文:
Manually piecing together JSON like this is pretty brittle. But here goes. A very common trick is to prefix each string except the first with a comma.
#!/bin/bash
IFS=':, ' read -r -a array <<< "$1"
prefix=''
printf '%s' '{"data":['
for item in "${array[@]}"; do
printf '%s%s' "$prefix" "{\"{#R_IP}\":\"$item\"}"
prefix=','
done
printf '%s\n' ']}'
Notice also how no explicit exit
is required at the end of a script. The shell stops executing the script and terminates when it reaches the end of the script.
Also, the shebang needs to start with exactly the two single-byte characters #!
.
Finally, a much better overall design is probably to not require the arguments to be comma-separated; but I won't try to fix that here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论