linux shell 指令碼擷取和替換檔案中特定內容

來源:互聯網
上載者:User

標籤:style   c   class   blog   code   tar   

1.從一串字串中擷取特定的資訊

要求1:擷取本機IP:menu.lst為系統鏡象的IP設定檔,需要從中擷取到本機IP資訊(從檔案擷取資訊)

1 timeout 12 default 03 4 title live5 find --set-root /casper/vmlinuz6 kernel /casper/vmlinuz boot=casper ignore_uuid showmounts ip=eth0,10.0.66.66,255.255.240.0,10.0.64.37 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 port11 UDPPort = 506012 #SCC domain ip13 Domain = 10.0.73.1414 CallTimeout = 6015 16 #RDS client working thread num (1--8)17 RDSClientThreadNum = 218 #RDS Client Log on|off19 RDSClientLog = off20 21 #Daemon process yes|no, default=yes22 IsDaemon = no23 24 #Web server addr25 WebServerUrl = http://localhost:900026 27 #SCC state Database28 DataBase = scc29 MySqlUserName = root30 MySqlUserPasswd [email protected]66631 32 MsIp = 127.0.0.133 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 $DOMAIN11     echo $LOCALIP12     #sed -e ‘s/Domain = 10.0.66.66/Domain = 10.0.73.15/g‘ $SCC_PATH13     sed -i ‘s/‘"${DOMAIN}"‘/Domain = ‘"${LOCALIP}"‘/g‘ $SCC_PATH14 }15 16 17 modify_conf()18 {19     modify_scc20 }21 22 modify_conf23 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 $DOMAIN11     echo $LOCALIP12     sed -i ‘s/‘"${DOMAIN}"‘/Domain = ‘"${LOCALIP}"‘/g‘ $SCC_PATH13 }14 15 modify_conf()16 {17     modify_scc18 }19 20 modify_conf21 22 exit 0
conf.sh
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.