消息关闭
    暂无新消息!
以下是我写的脚本
sed -i "s#$IP_old#$IP_new#g" $path/boomy_hz5.xml,用这句来执行替换文件中的IP功能,
但是一直没成功,
我还在之前的程序中加入了
echo $IP_old
echo $IP_new
这两个IP都是输出正常的,这证明两个变量是没错的
但就是sed没成功,于是我又在sed之后加了前一行是否成功的判断,结果执行时也显示sed是成功执行了,
但最终的结果在文件中并没有执行替换操作
请大神们斧正;PS我要替换的IP为IP_new=192.168.0.66,IP_old=192.168.0.70
#!/bin/bash
#auth by yingtie
#this script for dataserver auto start
#email:279096962@qq.com
#2016/06/12
#判断文件夹是否存在,若不存在则创建
if [ ! -d "/home/yuantiaotech/boomy/project" ]; then
        cp -r /tmp/boomy /home/yuantiaotech/boomy
        mkdir /home/yuantiaotech/boomy/project
fi
#断文件夹是否存在,若不存在则创建
if [ ! -d "/home/yuantiaotech/boomy/project/Storm" ]; then
  mv  /home/yuantiaotech/boomy/STS /home/yuantiaotech/boomy/project/Storm
fi
#断文件夹是否存在,若不存在则创建
if [ ! -d "/home/yuantiaotech/boomy/project/STD" ]; then
  mv  /home/yuantiaotech/boomy/STD /home/yuantiaotech/boomy/project/STD
fi
#mv /home/yuantiaotech/boomy/STS /home/yuantiaotech/boomy/Storm
#定义路径
path=/home/yuantiaotech/boomy/project/Storm
#从boomy_hz5.xml取出IP地址存入变量IP_old
IP_old=`tail $path/boomy_hz5.xml|sed -n '4p'|awk '{print $2}'|sed 's#name="url">jdbc:mysql://##g'|sed 's#:3306/boomy2.1.4##g'`
echo $IP_old
#取得本机IP存入变量IP_new
IP_new=`ifconfig|grep "inet addr:"|head -n 1|awk '{print $2}'|sed 's/addr://g'`
echo $IP_new
#用本机IP替换boomy_hz5.xml中的所有IP,完成后执行一个验证判断
sed -i "s#$IP_old#$IP_new#g" $path/boomy_hz5.xml
if [ $? -eq 0  ];then
        echo "\033[32mmysql&oracle jdbc in boomy_hz5.xml has been configured\033[0m"
else
        echo "\033[32mmysql&oracle jdbc in boomy_hz5.xml configure failed\033[0m"
fi
~     



下面是部分boomy_hz5.xml文件中的内容,请大家帮我分析下为啥没有替换成功
  <spout-configs>
                <config name="s_bayonet" device-type="bayonet">
                        <database type="oracle">
                                <property name="user">boomy</property>
                                <property name="password">boomy</property>
                                <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
                                <property name="jdbcUrl">jdbc:oracle:thin:@192.168.0.70:1521:orcl
                                </property>
                        </database>
                        <table name="TEST_SIMULATEBAYONETDATA">
                                <field name="ROAD_ID" />
                                <field name="NODE_ID" />
                                <field name="DATATIME" />
                        </table>
                </config>
                <config name="s_wave" device-type="wave">
                        <database type="oracle">
                                <property name="user">boomy</property>
                                <property name="password">boomy</property>
                                <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
                                <property name="jdbcUrl">jdbc:oracle:thin:@192.168.0.70:1521:orcl
                                </property>
                        </database>
                        <table name="TEST_SIMULATEWAVEDATA">
                                <field name="ROAD_ID" />
                                <field name="LANE_ID" />
                                <field name="DATATIME" />
                                                                                                      325,2-9       84%
                                <field name="LANE_ID" />
                                <field name="DATATIME" />
                                <field name="AVERAGEUSE" />
                                <field name="CARNUM" />
                        </table>
                </config>
                <config name="s_gps" device-type="gps">
                        <database type="oracle">
                                <property name="user">boomy</property>
                                <property name="password">boomy</property>
                                <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
                                <property name="jdbcUrl">jdbc:oracle:thin:@192.168.0.70:1521:orcl
                                </property>
                        </database>
                        <table name="TEST_SIMULATEGPSDATA">
                                <field name="TERMINAL" />
                                <field name="FLAG" />
                                <field name="DATATIME" />
                                <field name="LONGITUDE" />
                                <field name="LATITUDE" />
                                <field name="SPEED" />
                                <field name="RATE" />
                        </table>
                </config>
        </spout-configs>
        <bolt-configs>
                <config name="b_gps" source-type="GPS">
                        <rule name="speed" type="double">SPEED</rule>
                        <rule name="datatime" type="date">DATATIME</rule>
                        <rule name="originalId" type="string">TERMINAL</rule>
                        <rule name="workStatus" type="string">FLAG</rule>
                        <rule name="rate" type="double">RATE</rule>
                        <rule name="oriLongitude" type="double">LONGITUDE</rule>
                        <rule name="oriLatitude" type="double">LATITUDE</rule>
                        <rule name="count" type="int">1</rule>
                </config>
                <config name="b_flow" source-type="BAYONET">
                        <rule name="roadId" type="string">ROAD_ID</rule>
                                                                                                      361,5-33      93%
                <config name="b_flow" source-type="BAYONET">
<rule name="roadId" type="string">ROAD_ID</rule>
                        <rule name="nodeId" type="int">NODE_ID</rule>
                        <rule name="datatime" type="date">DATATIME</rule>
                        <rule name="originalId" type="string">ROAD_ID</rule>
                        <replace>roadId</replace>
                </config>
                <config name="b_occupany" source-type="WAVE">
                        <rule name="roadId" type="string">ROAD_ID</rule>
                        <rule name="laneId" type="int">LANE_ID</rule>
                        <rule name="averageuse" type="double">AVERAGEUSE</rule>
                        <rule name="waveflow" type="int">CARNUM</rule>
                        <rule name="datatime" type="date">DATATIME</rule>
                        <rule name="originalId" type="string">ROAD_ID</rule>
                        <replace>roadId</replace>
                </config>
        </bolt-configs>
        <topology-configs>
                <config name="url">jdbc:mysql://192.168.0.70:3306/boomy2.1.4
                </config>
                <config name="user">root</config>
                <config name="password">123456</config>
                <config name="flowCompetionFlag">true</config>
        </topology-configs>
</topology>
~              
 

7个回答

︿ 2
哦,是我错了,刚刚仔细看了一下,-i后面如果加了字符串才是后缀的意思。p不加也对的,p是作为输出。
我刚才又试了一下,貌似这样是没问题的,我用你的文件试了一下,也确实替换掉了。

︿ 0
sed -i "s   /    #$IP_old#$IP_new#g" $path/boomy_hz5.xml

要s后面有一个   "/"  的
︿ 0
我觉得你手动运行:
sed -i "s#192.168.0.70#192.168.0.66#g" boomy_hz5.xml
这样能确定问题是不是在这命令上