2016-12-24 149 views
-1

我想创建一个shell脚本,它将一些配置设置写入Ubuntu上的xml配置文件。但是,这些设置是针对MQ群集的,我需要脚本针对正在建立的每个节点循环不同的次数(由输入参数设置)。Shell Bash脚本在写入文件时循环

的XML我想写信给该文件是:

<listeners> 
     <tcp-listener> 
      <port>1883</port> 
      <bind-address>10.0.0.4</bind-address> 
     </tcp-listener> 
    </listeners> 
    <mqtt> 
     <max-client-id-length>65535</max-client-id-length> 
     <retry-interval>10</retry-interval> 
     <max-queued-messages>1000</max-queued-messages> 
    </mqtt> 
    <cluster> 
     <enabled>true</enabled> 
     <transport> 
      <tcp> 
       <bind-address>10.0.0.4</bind-address> 
       <bind-port>7800</bind-port> 
      </tcp> 
     </transport> 
     <discovery> 
      <static> 
       <node> 
        <host>10.0.0.5</host> 
        <port>7800</port> 
       </node> 
       <node> 
        <host>10.0.0.6</host> 
        <port>7800</port> 
       </node> 
       <node> n times </node> 
      </static> 
     </discovery> 
     <failure-detection> 
      <heartbeat> 
       <enabled>true</enabled> 
       <interval>5000</interval> 
       <timeout>15000</timeout> 
      </heartbeat> 
     </failure-detection> 
    </cluster> 

所以基本上,<node>对象的数量需要反映脚本采用在变量

但是,我不是。确定如何基于写入文件来循环。我正在研究使用tee命令,但这并不让我循环。我想我可以通过基于

在这里做写入文件写入到节点对象,然后循环是我到目前为止,仅仅写静态文本:

tee /opt/hivemq/conf/config.xml > /dev/null <<'EOF' 
    <the xml goes here> 
exit 0 
EOF 

有没有办法来循环在写?或者我需要写入循环对象,停止写入,然后有一个循环根据循环计数器进行多次写入,然后写入最后一位。

任何帮助将不胜感激。

+0

而你的问题是什么? –

+0

我还是很困惑。剧本应该循环什么?输出中应该重复什么?就我所知,您的XML示例不是重复的模式。你似乎也忘记了在第三段(?)中完成一个句子。 –

回答

0

有一个叫做“序列”,你可以用它来帮助迭代,那么你可以试试命令:

#!/bin/bash 

end=$1 

(
    echo start 

    for num in $(seq 1 $end) 
    do 
     node="10.0.0.$num" 
     echo $node 
    done 

    echo end 
) > out.xml 
+1

这是否真的回答了这个问题?顺便说一句,'seq'是无用的和非标准的。为什么一个子shell? –