将逗号分隔的命令行参数转换为Shell脚本中的JSON。

huangapple go评论93阅读模式
英文:

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=&#39;:, &#39; read -r -a array &lt;&lt;&lt; &quot;$1&quot;
idx=0
echo {\&quot;data\&quot;:[
while [ -n &quot;${array[$idx]}&quot; ]; do
        echo -n \{\&quot;{#R_IP}\&quot;:\&quot;&quot;${array[$idx]}&quot;\&quot;}
        let idx=$idx+1
        [ -n &quot;$array[idx]}&quot; ] &amp;&amp; echo &quot;,&quot; || 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

{&quot;data&quot;:[
{&quot;{#R_IP}&quot;:&quot;embimsrv.exe&quot;},
{&quot;{#R_IP}&quot;:&quot;emcms.exe&quot;},
{&quot;{#R_IP}&quot;:&quot;emcmsg.exe&quot;},
{&quot;{#R_IP}&quot;:&quot;emforecastsrv.exe&quot;},
{&quot;{#R_IP}&quot;:&quot;emgtw.exe&quot;},
{&quot;{#R_IP}&quot;:&quot;emguisrv.exe&quot;},
{&quot;{#R_IP}&quot;:&quot;emmaintag.exe&quot;},
{&quot;{#R_IP}&quot;:&quot;emselfservicesrv.exe&quot;},
{&quot;{#R_IP}&quot;:&quot;Naming_Service.exe&quot;},
{&quot;{#R_IP}&quot;:&quot;p_ctmce.exe&quot;},
{&quot;{#R_IP}&quot;:&quot;p_ctmcs.exe&quot;},
{&quot;{#R_IP}&quot;:&quot;p_ctmrt.exe&quot;},
{&quot;{#R_IP}&quot;:&quot;p_ctmtr.exe&quot;},
{&quot;{#R_IP}&quot;:&quot;p_ctmwd.exe&quot;},
]}

答案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 &#39;%s&#39; &quot;$1&quot; | jq -R &#39;split(&quot;,&quot;) | map({&quot;{#R_IP}&quot;: .}) | {data: .}&#39;

答案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=&#39;:, &#39; read -r -a array &lt;&lt;&lt; &quot;$1&quot;
prefix=&#39;&#39;
printf &#39;%s&#39; &#39;{&quot;data&quot;:[&#39;
for item in &quot;${array[@]}&quot;; do
    printf &#39;%s%s&#39; &quot;$prefix&quot; &quot;{\&quot;{#R_IP}\&quot;:\&quot;$item\&quot;}&quot;
    prefix=&#39;,&#39;
done
printf &#39;%s\n&#39; &#39;]}&#39;

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.

huangapple
  • 本文由 发表于 2020年1月3日 22:06:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定