-7
#!/bin/bash

host_name=A

echo -n " Enter a host name "
read host_name
total_hops=  traceroute $host_name | cut -d " " -f 1 | wc -l
read $total_hops
echo "The host $host_name is $total_hops away"
alexroussos
  • 2,671
  • 1
  • 25
  • 38
Troy
  • 1
  • 1

1 Answers1

0

Two things don't make sense to me about your script: first, you don't need read $total_hops because you're already assigning it; second, if you just want to count the lines, you can just use wc without cut. But seem to be some extra lines in there that should not count as hops, so I would get the last line using tail and then use the cut to get the first column of that:

#!/bin/bash

echo -n "Enter a host name "
read host_name
total_hops=`traceroute $host_name | tail -n 1 | cut -d " " -f 1 `
echo "The host $host_name is $total_hops hops away"

The output is:

> Enter a host name google.com 
> traceroute to google.com (216.58.216.46), 64 hops max, 52 byte packets 
> The host google.com is 12 hops away
alexroussos
  • 2,671
  • 1
  • 25
  • 38