Piduna Valeriu Piduna Valeriu - 27 days ago 16
Bash Question

Centos add multiple interfaces from file to VLANs

I write script, that will create CentOS interfaces from file with list of IP addresses. In loop, i create file, next action, i add data for centos interfaces. Look:

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


All ok. But it is not working. When i start bash/sh -x, i receive this:

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


Where i have error ? Please help. In ubuntu it is simply, because all will write in one file. But in CentOS, it is too difficult for me.

Answer

When you write:

variable=`command`

it sets the variable to the output of the command. But touch doesn't produce any output, so

cent_int=`touch /etc/sysconfig/network-scripts/ifcfg-$inter:$eth_temp`

assigns an empty string to cent_int. I think what you want is:

cent_int=/etc/sysconfig/network-scripts/ifcfg-$inter:$eth_temp

You don't need to use touch, since writing to the file with cat >> $cent_int will create the file if it doesn't already exist.

Comments