0

Good afternoon,

im playing around with bash and wanted to write a script that would perform a traceroute and check if the second hop was a specific IP address. below is my script:

Failover_check= traceroute 8.8.8.8 -n | grep 192.168.0.2 | awk '{print  $2;}'

if [ $Failover_check = "192.168.0.2" ]
then
    echo "ip found"
else
    echo "ip not ound"
fi

every time I run the script it always hits the else statement

Thanks

SriniV
  • 10,691
  • 14
  • 59
  • 88

2 Answers2

3

Assign it to the variable using $(...) and make sure no space before and after = that must avoid split and provide proper result

Failover_check=$(traceroute 8.8.8.8 -n | grep 192.168.0.2 | awk '{print  $2;}')
SriniV
  • 10,691
  • 14
  • 59
  • 88
2

because the output of your command is not assigned to Failover_check variable. Use:

Failover_check=$(traceroute 8.8.8.8 -n | grep 192.168.0.2 | awk '{print  $2;}')

also; you might consider quoting your variables; to avoid errors when the variable is empty:

if [ "$Failover_check" = "192.168.0.2" ]
Chris Maes
  • 30,644
  • 6
  • 98
  • 124