0

I'm running a loop like this:

for ip in 10.0.0.1 10.0.0.2 10.0.0.3; do traceroute $ip; done

I want to be able to kill the current traceroute when it gets boring (and before its 30-hop max), and move on to the next one. But when I press Ctrl+c, it kills the whole for loop.

I guess what I need is either a way to end the current traceroute, or to send the loop a continue.

This question and this one are doing a similar thing, but in more complicated situations. Is there a reasonable way to do this when just running stuff from the prompt?

Jacktose
  • 441

1 Answers1

2

Use trap:

trap "echo ctr+c pressed" INT TERM;for ip in 10.0.0.1 10.0.0.2 10.0.0.3; do traceroute $ip; done
Ipor Sircer
  • 14,546
  • 1
  • 27
  • 39
  • Ah, that does just what I wanted! The link is useful, but can you explain how it works here? – Jacktose Jun 26 '18 at 00:45
  • Read the fine manual or use google. There are a tons of paper about this. – Ipor Sircer Jun 26 '18 at 00:58
  • I do appreciate the answer. Just trying to encourage the "Provide context for links" advice from https://unix.stackexchange.com/help/how-to-answer – Jacktose Jun 26 '18 at 04:51