linux shell 脚本获取和替换文件中特定内容

1.从一串字符串中获取特定的信息

要求1:获取本机IP:menu.lst为系统镜象的IP配置文件,需要从中获取到本机IP信息(从文件获取信息)

1 timeout 1
2 default 0
3 
4 title live
5 find --set-root /casper/vmlinuz
6 kernel /casper/vmlinuz boot=casper ignore_uuid showmounts ip=eth0,10.0.66.66,255.255.240.0,10.0.64.3
7 initrd /casper/initrd.lz
menu.lst

要求2:修改(替换)原文件中特定内容:CenterServer.conf为SCC可执行程序的配置文件,需要将配置文件中的domain值改为本机IP。

 1 #CenterServer config file
 2 
 3 # Logging Type: syslog|cerr|cout|file
 4 LoggingType = cout
 5 
 6 # Logging level: NONE|CRIT|ERR|WARNING|INFO|DEBUG|STACK
 7 LogLevel = DEBUG
 8 #LogLevel = INFO
 9 
10 #sip listen port
11 UDPPort = 5060
12 #SCC domain ip
13 Domain = 10.0.73.14
14 CallTimeout = 60
15 
16 #RDS client working thread num (1--8)
17 RDSClientThreadNum = 2
18 #RDS Client Log on|off
19 RDSClientLog = off
20 
21 #Daemon process yes|no, default=yes
22 IsDaemon = no
23 
24 #Web server addr
25 WebServerUrl = http://localhost:9000
26 
27 #SCC state Database
28 DataBase = scc
29 MySqlUserName = root
30 MySqlUserPasswd =jonet@666
31 
32 MsIp = 127.0.0.1
33 MsPort = 9999
CenterServer.conf

实现脚本:

 1 #!/bin/sh
 2 MENULST="/JONET_boot/menu.lst"
 3 SCC_PATH="/JONET/bin/SCC/CenterServer.config"
 4 LOCALIP="`awk -F ‘,‘ ‘{print $2}‘ $MENULST |grep .`";
 5 echo $LOCALIP
 6 
 7 modify_scc()
 8 {
 9     DOMAIN="`cat $SCC_PATH|grep Domain`"
10     echo $DOMAIN
11     echo $LOCALIP
12     #sed -e s/Domain = 10.0.66.66/Domain = 10.0.73.15/g $SCC_PATH
13     sed -i s/‘"${DOMAIN}"‘/Domain = ‘"${LOCALIP}"‘/g $SCC_PATH
14 }
15 
16 
17 modify_conf()
18 {
19     modify_scc
20 }
21 
22 modify_conf
23 
24 exit 0
conf.sh

说明:sed -i ‘s/‘"${DOMAIN}"‘/Domain = ‘"${LOCALIP}"‘/g‘ $SCC_PATH

  由于宏定义DOMAIN和LOCALIP在命令中使用$DOMAIN和$LOCALIP无法使用,需要使用‘"${DOMAIN}"‘和‘"${LOCALIP}"‘,使执行脚本时能够替换

 1 #!/bin/sh
 2 MENULST="/JONET_boot/menu.lst"
 3 SCC_PATH="/JONET/bin/SCC/CenterServer.config"
 4 LOCALIP="`awk -F ‘,‘ ‘{print $2}‘ $MENULST |grep .`";
 5 echo $LOCALIP
 6 
 7 modify_scc()
 8 {
 9     DOMAIN="`cat $SCC_PATH|grep Domain`"
10     echo $DOMAIN
11     echo $LOCALIP
12     sed -i s/‘"${DOMAIN}"‘/Domain = ‘"${LOCALIP}"‘/g $SCC_PATH
13 }
14 
15 modify_conf()
16 {
17     modify_scc
18 }
19 
20 modify_conf
21 
22 exit 0
conf.sh

linux shell 脚本获取和替换文件中特定内容,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。