Centos将多个接口从文件添加到VLAN

时间:2016-11-07 22:00:37

标签: linux bash shell scripting sh

我编写脚本,将从文件中创建具有IP地址列表的CentOS接口。在循环中,我创建文件,下一步操作,我为centos接口添加数据。看:

from=/root/ip
inter=`cat /proc/net/dev | grep "eth0:\|venet0" |  awk '{ print $1 }' | sed 's/://g'`
eth=`ifconfig | grep $inter | tail -1 | awk '{ print $1 }' | sed "s/$inter://g"`
echo "Last number of interface: $eth"

if [ "$eth" == "eth0" ]; then
 eth_temp="-1"
else
 eth_temp=$eth
fi

if [ "$inter" == "eth0" ]; then
 echo "Name of interface: $inter"
 echo "Add IP to interfaces"
if [ -f $from ]; then
 for IP_TO_ETH in `grep -v ^# $from`; do
 eth_temp=$(($eth_temp+1))
 cent_int=`touch /etc/sysconfig/network-scripts/ifcfg-$inter:$eth_temp`
 cat >> $cent_int <<END
DEVICE=eth0:$eth_temp
ONBOOT=yes
BOOTPROTO='static'
IPADDR=$IP_TO_ETH
NETMASK=255.255.255.0

END
  done
 else
 echo "File does not exist"
fi
elif [ "$inter" == "venet0" ]; then
 echo "Name of interface: $inter"
 echo "This interface from OpenVZ. Not need to add"
 else
 echo "Other name of inteface"
fi

一切都好。但它没有用。当我启动bash / sh -x时,我会收到:

cent.sh: line 28: $cent_int: ambiguous redirect
+ for IP_TO_ETH in '`grep -v ^# $from`'
+ eth_temp=61
++ touch /root/network-scripts/ifcfg-eth0:61
+ cent_int=
+ cat
cent.sh: line 28: $cent_int: ambiguous redirect
+ for IP_TO_ETH in '`grep -v ^# $from`'
+ eth_temp=62
++ touch /root/network-scripts/ifcfg-eth0:62
+ cent_int=
+ cat

哪里有错误?请帮忙。在ubuntu中它很简单,因为所有都将写入一个文件。但在CentOS中,对我来说太难了。

1 个答案:

答案 0 :(得分:1)

当你写:

touch

它将变量设置为命令的输出。但是cent_int=`touch /etc/sysconfig/network-scripts/ifcfg-$inter:$eth_temp` 不会产生任何输出,所以

cent_int

将空字符串分配给cent_int=/etc/sysconfig/network-scripts/ifcfg-$inter:$eth_temp 。我想你想要的是:

touch

您不需要使用cat >> $cent_int,因为使用{{1}}写入文件会创建该文件(如果该文件尚不存在)。