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

Srini V
  • 11,045
  • 14
  • 66
  • 89

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;}')
Srini V
  • 11,045
  • 14
  • 66
  • 89
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
  • 35,025
  • 12
  • 111
  • 136