Json 在循环中添加按元素命名的值

Json 在循环中添加按元素命名的值,json,bash,Json,Bash,我有下一段代码: echo -n "{ \"data\":" > test.json echo -n " [" >> test.json sqcur=0 sqtotal=`ls /etc/squid?.conf | wc -l` tcp_ports=$(grep "port" /etc/squid?.conf |awk '{print $2}' |sed 's|.*[0-9]*\:||g'|sort|uniq|xargs) for squidconf in `ls /et

我有下一段代码:

echo -n "{ \"data\":" > test.json
echo -n "  [" >> test.json

sqcur=0
sqtotal=`ls /etc/squid?.conf | wc -l`
tcp_ports=$(grep "port" /etc/squid?.conf |awk '{print $2}' |sed 's|.*[0-9]*\:||g'|sort|uniq|xargs)

for squidconf in `ls /etc/squid?.conf`; do
  let "sqcur+=1" > /dev/null
  squidident=`echo $squidconf | sed 's/.*\///' | awk -F '.' '{print $1}'`
  tcp_ports=$(grep "port" $squidconf |awk '{print $2}' |sed 's|.*[0-9]*\:||g'|sort|uniq|xargs)  
  if [ $sqcur -lt $sqtotal ]; then
    echo -n "    { \"{#SQPROC}\": \"/usr/local/squid/bin/squid\", \"{#SQPROC_IDENT}\": \"${squidident}\", \"{#SQPROC_ARGS}\": \"-D -C -F -f ${squidconf}\", \"{#SQPROC_PORT}\": \"${tcp_ports}\", \"{#SQPROC_CONF}\": \"${squidconf}\" }," >> test.json
  else
    echo -n "    { \"{#SQPROC}\": \"/usr/local/squid/bin/squid\", \"{#SQPROC_IDENT}\": \"${squidident}\", \"{#SQPROC_ARGS}\": \"-D -C -F -f ${squidconf}\", \"{#SQPROC_PORT}\": \"${tcp_ports}\", \"{#SQPROC_CONF}\": \"${squidconf}\" }" >> test.json  
  fi
done

echo -n "  ]" >> test.json
echo "}" >> test.json
它给出了json文件中的下一个内容:

{
        "data": [
            {
                "{#SQPROC}": "/usr/local/squid/bin/squid",
                "{#SQPROC_IDENT}": "squid1",
                "{#SQPROC_ARGS}": "-D -C -F -f /etc/squid1.conf",
                "{#SQPROC_PORT}": "1111 2222",
                "{#SQPROC_CONF}": "/etc/squid1.conf"
            },
但我希望在端口有多个值的情况下获得下一个结果:

{
        "data": [
            {
                "{#SQPROC}": "/usr/local/squid/bin/squid",
                "{#SQPROC_IDENT}": "squid1",
                "{#SQPROC_ARGS}": "-D -C -F -f /etc/squid1.conf",
                "{#SQPROC_PORT_1111}": "1111",
                "{#SQPROC_PORT_2222}": "2222",
                "{#SQPROC_CONF}": "/etc/squid1.conf"
            },

你能帮忙吗

您可能正在寻找一个类似这样的

for port in $tcpports
do
     all_ports="${all_ports} \"{#SQPROC_PORT_$port}\": \"$port\","
done
在此循环结束时,您将有一个
all\u port
变量,其中包含以下内容:

"{#SQPROC_PORT_1111}": "1111", "{#SQPROC_PORT_2222}": "2222",
比如说

为了在您已有的代码中轻松集成,我建议对
for
-循环进行一些小的更改。对于每个迭代,构建一个变量
json\u content
,该变量将包含该迭代时的json数据,在迭代结束时,只需打印
$json\u content

echo -n "{ \"data\":" > test.json
echo -n "  [" >> test.json

sqcur=0
sqtotal=`ls /etc/squid?.conf | wc -l`

for squidconf in `ls /etc/squid?.conf`; do
    let "sqcur+=1" > /dev/null
    squidident=`echo $squidconf | sed 's/.*\///' | awk -F '.' '{print $1}'`
    tcp_ports=$(grep "port" $squidconf |awk '{print $2}' |sed 's|.*[0-9]*\:||g'|sort|uniq|xargs)  
    json_content="{ \"{#SQPROC}\": \"/usr/local/squid/bin/squid\", \"{#SQPROC_IDENT}\": \"${squidident}\", \"{#SQPROC_ARGS}\": \"-D -C -F -f ${squidconf}\","
    for port in $tcp_ports
    do
         json_content="$json_content \"{#SQPROC_PORT_$port}\": \"$port\","
    done
    json_content="$json_content \"{#SQPROC_CONF}\": \"${squidconf}\" }"
    if [ $sqcur -lt $sqtotal ]; then
        json_content="${json_content},"
    fi
    echo -n "    ${json_content}" >> test.json
done

echo -n "  ]" >> test.json
echo "}" >> test.json
我无法验证循环的其余部分。我没有乌贼!但是,当您重新计算for循环中的
tcp\u端口时,我删除了for循环之前的初始化

希望这有帮助,

祝你好运

听起来像是程序员的工作。