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

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

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

 timeout
default title live
find --set-root /casper/vmlinuz
kernel /casper/vmlinuz boot=casper ignore_uuid showmounts ip=eth0,10.0.66.66,255.255.240.0,10.0.64.3
initrd /casper/initrd.lz

menu.lst

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

 #CenterServer config file

 # Logging Type: syslog|cerr|cout|file
LoggingType = cout # Logging level: NONE|CRIT|ERR|WARNING|INFO|DEBUG|STACK
LogLevel = DEBUG
#LogLevel = INFO #sip listen port
UDPPort =
#SCC domain ip
Domain = 10.0.73.14
CallTimeout = #RDS client working thread num (--)
RDSClientThreadNum =
#RDS Client Log on|off
RDSClientLog = off #Daemon process yes|no, default=yes
IsDaemon = no #Web server addr
WebServerUrl = http://localhost:9000 #SCC state Database
DataBase = scc
MySqlUserName = root
MySqlUserPasswd =jonet@ MsIp = 127.0.0.1
MsPort =

CenterServer.conf

实现脚本:

 #!/bin/sh
MENULST="/JONET_boot/menu.lst"
SCC_PATH="/JONET/bin/SCC/CenterServer.config"
LOCALIP="`awk -F ',' '{print $2}' $MENULST |grep .`";
echo $LOCALIP modify_scc()
{
DOMAIN="`cat $SCC_PATH|grep Domain`"
echo $DOMAIN
echo $LOCALIP
#sed -e 's/Domain = 10.0.66.66/Domain = 10.0.73.15/g' $SCC_PATH
sed -i 's/'"${DOMAIN}"'/Domain = '"${LOCALIP}"'/g' $SCC_PATH
} modify_conf()
{
modify_scc
} modify_conf exit

conf.sh

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

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

 #!/bin/sh
MENULST="/JONET_boot/menu.lst"
SCC_PATH="/JONET/bin/SCC/CenterServer.config"
LOCALIP="`awk -F ',' '{print $2}' $MENULST |grep .`";
echo $LOCALIP modify_scc()
{
DOMAIN="`cat $SCC_PATH|grep Domain`"
echo $DOMAIN
echo $LOCALIP
sed -i 's/'"${DOMAIN}"'/Domain = '"${LOCALIP}"'/g' $SCC_PATH
} modify_conf()
{
modify_scc
} modify_conf exit

conf.sh

上一篇:HTML之学习笔记(七)列表


下一篇:JavaScript基础15——js的DOM对象