The ability to obtain the network service name programatically is useful in conjuntion with networksetup (see my previous hint on that). In my case, I'm working on a script that opens a socks proxy with ssh -D. I want the script to also insert the necessary configuration at the system level, so that my applications will then use the proxy. Getting the current network service name means that the script will work no matter how I am connected to the Internet. This is because the network service name must be supplied to networksetup when modifying the proxy configuration, and the network service name may change with network location. I will possibly post that script once it's finished, but I think this is useful as it stands for rolling your own scripts around networksetup.
I couldn't get carriage returns to be interpreted correctly in the echo statements (well, I couldn't after 10 minutes of trying, then I gave up), which makes this messier than it needs to be. I tried several combinations of printf and echo, but I couldn't get the newline to come through. printf would have worked except it apparently doesn't do variable interpolation. Feel free to post any improvements as comments here.

