Мне нужно сравнить две переменные / интерфейсы, и это не работает - PullRequest
0 голосов
/ 17 ноября 2018
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 голосов
/ 17 ноября 2018

Ваш код всегда будет сообщать "Адрес не совпадает".Если адрес соответствует $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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...