我需要比较两个变量/接口,但它不起作用

时间:2018-11-17 16:58:33

标签: bash shell unix

IPv4=$( ifconfig |grep -v 'eth1:' |grep -A 1 'eth1'| tail -1 |cut -d ':' -f 2 |cut -d ' ' -f 1)
IPnode1=$"111.22.333.44"
IPnode2=$"111.22.333.45"


ifconfig |grep -v 'eth1:' |grep -A 1 'eth1'| tail -1 |cut -d ':' -f 2 |cut -d ' ' -f 1

if [[ "$IPv4" = "$IPnode1" ]]; then

   echo "found the address "
   echo "111.22.333.44   VM01.com VM01" >> /etc/hosts

else

   echo "The address does not match"

fi

  ifconfig |grep -v 'eth1:' |grep -A 1 'eth1'| tail -1 |cut -d ':' -f 2 |cut -d ' ' -f 1

if [[ "$IPv4" = "$IPnode2" ]]; then


      echo ""
      echo "found the address "
      echo "111.22.333.45   VM02.com VM02" >> /etc/hosts

else

   echo "The address does not match"

fi

1 个答案:

答案 0 :(得分:0)

您的代码将始终报告“地址不匹配”。如果地址与$IPnode1相匹配,则与$IPnode2不匹配,反之亦然。仅当第一个测试失败时,才应执行第二个测试。

if [[ "$IPv4" = "$IPnode1" ]]; then
    echo "found the address "
    echo "$IPnode1   VM01.com VM01" >> /etc/hosts
elif [[ "$IPv4" = "$IPnode2" ]]; then
    echo "found the address "
    echo "$IPnode2   VM02.com VM02" >> /etc/hosts
else
    echo "The address does not match"
fi