我有一个小文件:
# Format
# <iface_name>,<ip_addr>,<ip_netmask>,<broadcast>,<eth_addr>,<status>,<obtaining_method>
eth0,192.168.0.11,255.255.255.0,192.168.0.255,4A:DD:00:00:05:00,up,static
lv0,172.20.8.12,255.255.248.0,172.20.15.255,4A:DD:00:FF:15:01,down,static
#lv0:0,172.20.16.12,255.255.248.0,172.20.23.255,4A:DD:00:FF:15:01,down,static
#lv0:1,172.20.24.12,255.255.248.0,172.20.31.255,4A:DD:00:FF:15:01,down,static
lv1,172.21.8.11,255.255.248.0,172.21.15.255,4A:DD:00:FF:15:02,down,dhcp
#lv1:0,172.21.16.1,255.255.248.0,172.21.23.255,4A:DD:00:FF:15:02,down,static
#lv1:1,172.21.24.1,255.255.248.0,172.21.31.255,4A:DD:00:FF:15:02,down,static
mv0,10.100.0.1,255.255.255.0,10.100.0.255,40:FD:00:00:01:0A,up,static
lp0,172.22.0.12,255.255.255.0,172.22.0.255,4A:DD:00:FF:15:04,down,static
lp1,172.22.1.12,255.255.255.0,172.22.1.255,4A:DD:00:FF:15:05,down,static
dummy0,192.168.111.100,255.255.255.0,192.168.111.255,00:00:00:00:00:00,down,static
我有一个想法,在一个周期内获取所有接口参数,并做了这个表达式:
#!/bin/sh
SPATH=/mnt/dcu/tmp/
for INTERFACE in $(awk -F\, '/^[^#]/ {print $1}' $SPATH/ifaces); do
INTERFACE_SETTINGS=$(awk -F\, '$1 ~ interface_name {print $2,$3,$4,$5,$6,$7}' interface_name=$INTERFACE $SPATH/ifaces)
set $INTERFACE_SETTINGS
IPADDR=$1
NETMASK=$2
BROADCAST=$3
MACADDR=$4
STATE=$5
METHOD=$6
sed -r -i -e "/$INTERFACE/s/^([^,]*,)([^,]*,)([^,]*,)([^,]*,)([^,]*,)([^,]*,)([^,]*)$/$INTERFACE,$IPADDR,$NETMASK,$BROADCAST,$MACADDR,$STATE,$METHOD/" /etc/network/ifaces
done
但它执行以下操作:
# Format
# <iface_name>,<ip_addr>,<ip_netmask>,<broadcast>,<eth_addr>,<status>,<obtaining_method>
eth0,192.168.0.11,255.255.255.0,192.168.0.255,4A:DD:00:00:05:00,up,static
lv0,172.20.8.12,255.255.248.0,172.20.15.255,4A:DD:00:FF:15:01,down,static
lv0,172.20.8.12,255.255.248.0,172.20.15.255,4A:DD:00:FF:15:01,down,static
lv0,172.20.8.12,255.255.248.0,172.20.15.255,4A:DD:00:FF:15:01,down,static
lv1,172.21.8.11,255.255.248.0,172.21.15.255,4A:DD:00:FF:15:02,down,dhcp
lv1,172.21.8.11,255.255.248.0,172.21.15.255,4A:DD:00:FF:15:02,down,dhcp
lv1,172.21.8.11,255.255.248.0,172.21.15.255,4A:DD:00:FF:15:02,down,dhcp
mv0,10.100.0.1,255.255.255.0,10.100.0.255,40:FD:00:00:01:0A,up,static
lp0,172.22.0.12,255.255.255.0,172.22.0.255,4A:DD:00:FF:15:04,down,static
lp1,172.22.1.12,255.255.255.0,172.22.1.255,4A:DD:00:FF:15:05,down,static
dummy0,192.168.111.100,255.255.255.0,192.168.111.255,00:00:00:00:00:00,down,static
它会得到更改和已评论的接口。
我试图为它做任何例外 - 但它不适用于一行:
awk -F\, '{ if ($1 !~ /[#]|^$/interface_name ) {print $2,$3,$4,$5,$6,$7} }' interface_name=lv0 /etc/network/ifaces
192.168.0.1 255.255.255.0 192.168.0.255 4A:DD:00:00:01:0A up static
172.20.8.1 255.255.248.0 172.20.15.255 4A:DD:00:FF:15:01 up static
172.21.8.1 255.255.248.0 172.21.15.255 4A:DD:00:FF:15:02 down static
10.10.0.1 255.255.255.0 10.10.0.255 40:FD:00:00:01:0A down static
172.22.0.1 255.255.255.0 172.22.0.255 4A:DD:00:FF:15:04 down static
172.22.1.1 255.255.255.0 172.22.1.255 4A:DD:00:FF:15:05 down static
192.168.111.111 255.255.255.0 192.168.111.255 00:00:00:00:00:00 down static
我怎么能为我的案子做呢?
先感谢您。