Sorry Pädu but this smells for many reasons.. first:<p><pre><code> The dns-sd command is primarily intended for interactive use. Because
its command-line arguments and output format are subject to change,
invoking it from a shell script will generally be fragile. Additionally,
the asynchronous nature of DNS Service Discovery does not lend itself
easily to script-oriented programming. For example, calls like "browse"
never complete;
</code></pre>
Second, "SIGKILL is too verbose, SIGINT is too soft".. how about SIGTERM? Its very likely that you're doing it wrong if you need to use -KILL in your scripts.<p>Third, "[1] 58181 killed ./discover-vnc.sh --- It's crucial to suppress this line." again, if you think you must suppress this, you're doing it wrong. This shouldn't pop up in the first place.<p>Fourth, what about fifos and `$!'?<p>Fifth, why bash? Use the sh-subset.<p>etc.<p>It's cute that you managed to solve your problem but this is nothing more than an unreliable hack, pompously presented ("Taming non-terminating Bash processes", "Kill the children"..)
The use of <(..) is really weird here... why not just pipe dns-sd to the while loop?<p><pre><code> dns-sd | while read -r line ; do
...
done
</code></pre>
Sure, you won't be able to accumulate the output for the trap, but you shouldn't <i>need</i> to---this is stream processing!<p>Consider replacing the while loop with awk. This sort of thing is exactly what awk is good at:<p><pre><code> BEGIN { FS = " "; count = 0; }
NR > 5 {
count++
print
if ($3 != "3") { exit; }
}
END { printf("%d hosts found\n", count); }
</code></pre>
You could also do it with GNU sed (you'll need the q or Q command for early exit, and that's a GNU extension---no idea if it's available on OS X).
That's all well and good, but the real issue is that dns-sd is really meant to be used from a shell script<i>.<p>It should be noted that Linux's command line program to enumerate network services, avahi-browse, includes a '-t' flag so that terminates after dumping service entries.<p></i><a href="https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/dns-sd.1.html" rel="nofollow">https://developer.apple.com/library/mac/documentation/Darwin...</a>