溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

sed運(yùn)用實(shí)例一——基于變量的動(dòng)態(tài)替換

發(fā)布時(shí)間:2020-07-17 21:18:46 來(lái)源:網(wǎng)絡(luò) 閱讀:1334 作者:tiancong 欄目:系統(tǒng)運(yùn)維

在工作中我需要修改兩個(gè)文件:

文件一:/etc/vmware/networking

  1. VERSION=1,0 
  2. answer VNET_1_DHCP yes 
  3. answer VNET_1_DHCP_CFG_HASH 50CE8453B1072EA401BFFA704E6C01F7AE0BA67C 
  4. answer VNET_1_HOSTONLY_NETMASK 255.255.255.0 
  5. answer VNET_1_HOSTONLY_SUBNET 172.16.77.0 
  6. answer VNET_1_VIRTUAL_ADAPTER yes 
  7. answer VNET_8_DHCP yes 
  8. answer VNET_8_DHCP_CFG_HASH 1DA1A936DCB3AA3313CF293A9C96D25B76AB48A2 
  9. answer VNET_8_HOSTONLY_NETMASK 255.255.255.0 
  10. answer VNET_8_HOSTONLY_SUBNET 192.168.43.0 
  11. answer VNET_8_NAT yes 
  12. answer VNET_8_VIRTUAL_ADAPTER yes 

文件二:/etc/vmware/vmnet8/dhcpd/dhcpd.conf

  1. # Configuration file for ISC 2.0 vmnet-dhcpd operating on vmnet8. 
  2. # This file was automatically generated by the VMware configuration program. 
  3. # See Instructions below if you want to modify it. 
  4. # We set domain-name-servers to make some DHCP clients happy 
  5. # (dhclient as configured in SuSE, TurboLinux, etc.). 
  6. # We also supply a domain name to make pump (Red Hat 6.x) happy. 
  7.  
  8.  
  9. ###### VMNET DHCP Configuration. Start of "DO NOT MODIFY SECTION" ##### 
  10. # Modification Instructions: This section of the configuration file contains 
  11. # information generated by the configuration program. Do not modify this 
  12. # section. 
  13. # You are free to modify everything else. Also, this section must start  
  14. # on a new line  
  15. # This file will get backed up with a different name in the same directory  
  16. # if this section is edited and you try to configure DHCP again. 
  17.  
  18. # Written at: 03/22/2013 15:49:55 
  19. allow unknown-clients; 
  20. default-lease-time 1800;                # default is 30 minutes 
  21. max-lease-time 7200;                    # default is 2 hours 
  22.  
  23. subnet 192.168.43.0 netmask 255.255.255.0 { 
  24.     range 192.168.43.128 192.168.43.254; 
  25.     option broadcast-address 192.168.43.255; 
  26.     option domain-name-servers 192.168.43.2; 
  27.     option domain-name localdomain; 
  28.     default-lease-time 1800;                # default is 30 minutes 
  29.     max-lease-time 7200;                    # default is 2 hours 
  30.     option netbios-name-servers 192.168.43.2; 
  31.     option routers 192.168.43.2; 
  32. host vmnet8 { 
  33.     hardware ethernet 00:50:56:C0:00:08; 
  34.     fixed-address 192.168.43.1; 
  35.     option domain-name-servers 0.0.0.0; 
  36.     option domain-name ""; 
  37.     option routers 0.0.0.0; 
  38. ####### VMNET DHCP Configuration. End of "DO NOT MODIFY SECTION" ####### 

需求:

將兩個(gè)文件中的"192.168.43"修改為"202.16.22",不過(guò),有時(shí)"192.168.43"會(huì)表現(xiàn)為其它的IP值,而要修改的目標(biāo)字串"202.16.22"也可能會(huì)隨著環(huán)境的改變另行設(shè)置.

基于此需求,本人編寫(xiě)了如下測(cè)試腳本:

針對(duì)文件一:

  1. #!/bin/bash 
  2. # $vmnat_subnet_old變量表示vmnat8原有的子網(wǎng)配置,注意只截取前三段 
  3. vmnat_subnet_old=`grep "VNET_8_HOSTONLY_SUBNET" /etc/vmware/networking | sed 's/..$//' | cut -d' ' -f 3` 
  4. # $vmnat_subnet_new變量表示vmnat8子網(wǎng)的目標(biāo)配置,注意只截取前三段 
  5. vmnat_subnet_new='192.168.40' 
  6.  
  7. cp /etc/vmware/networking /etc/vmware/networking_bak0 
  8. sed -i '/VNET_8_HOSTONLY_SUBNET/s/'"$vmnat_subnet_old"'/'"$vmnat_subnet_new"'/' /etc/vmware/networking 
  9. grep "$vmnat_subnet_new" /etc/vmware/networking 
  10. cp /etc/vmware/networking_bak0 /etc/vmware/networking 

針對(duì)文件二:

  1. #!/bin/bash 
  2. # $vmnat_subnet_new變量表示vmnat8子網(wǎng)的目標(biāo)配置,注意只截取前三段 
  3. vmnat_subnet_new='192.168.40' 
  4.  
  5. # $vmnat_subnet_old變量表示vmnat8原有的子網(wǎng)配置,注意只截取前三段 
  6. vmnat_subnet_old=`grep "VNET_8_HOSTONLY_SUBNET" /etc/vmware/networking | sed 's/..$//' | cut -d' ' -f 3` 
  7.  
  8. cat /etc/vmware/vmnet8/dhcpd/dhcpd.conf 
  9. cp /etc/vmware/vmnet8/dhcpd/dhcpd.conf /etc/vmware/vmnet8/dhcpd/dhcpd.conf_bak0 
  10. sed -i '/'"$vmnat_subnet_old"'/s//'"$vmnat_subnet_new"'/g' /etc/vmware/vmnet8/dhcpd/dhcpd.conf 
  11. grep "$vmnat_subnet_new" /etc/vmware/vmnet8/dhcpd/dhcpd.conf 
  12. cp /etc/vmware/vmnet8/dhcpd/dhcpd.conf_bak0 /etc/vmware/vmnet8/dhcpd/dhcpd.conf 

優(yōu)點(diǎn):

可以不用事先知道要替換的字符是(本例中:192.168.43)什么,只需要知道自己想替換為(本例中:202.16.22)什么就可以了。

 呵呵,我的工作桌面截圖。

sed運(yùn)用實(shí)例一——基于變量的動(dòng)態(tài)替換

 

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI