- 论坛徽章:
- 0
|
本帖最后由 winson1740 于 2015-05-11 13:51 编辑
有若干个文件:/sys/class/net/ethX/adress ,其中X为0-99的数字,每个文件的内容只有1行,但各不相同,如/sys/class/net/eth0/adress文件的格式如下:
00:0c:26:2c:48:e7
有文件b_file内容如下:
# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:11:22:33:aa:ef", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:11:29:2f:48:e5", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:11:29:2f:48:f9", ATTR{type}=="1", KERNEL=="eth*", NAME="eth2"
# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:11:29:2f:48:03", ATTR{type}=="1", KERNEL=="eth*", NAME="eth3"
现在想用/sys/class/net/下的目录名ethX(我设置的变量名为eth_name)去匹配b_file中的NAME="ethX",然后用/sys/class/net/ethX/adress的内容去替换b_file匹配行第四列中的mac地址(如"00:11:29:2f:48:e5"替换为"00:0c:26:2c:48:e7",其他不变,直到b_file中的mac(我设置的变量名为mac_address)都被替换完。目前写好了shell循环结构,但替换操作不知如何实现,请各位指导,谢谢!
cd /sys/class/net
ls -l eth* | awk '{print $9}' | while read eth_name
do
cat /sys/class/net/$eth_name/address | while read mac_address
do
替换操作
done
done
|
|