#!/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"
Asked
Active
Viewed 1,596 times
-7

alexroussos
- 2,671
- 1
- 25
- 38

Troy
- 1
- 1
-
Have a look at http://stackoverflow.com/questions/1183625/parsing-data-from-traceroute-command – alexroussos Apr 19 '15 at 00:36
1 Answers
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