unitedwallabies
Joined: 23 Sep 2011 Posts: 8 Location: North-Eastern US
|
Posted: Wed Oct 26, 2011 6:51 pm Post subject: Improving the "external-ip" script |
|
|
The one-line script invokes `upnpc -s' and parses the output looking for line(s) matching "ExternalIP". The parsing currently uses both grep and sed. Combining grep, cut, awk, and sed on the same pipe-line, though sometimes convenient, is always sub-optimal -- it is Ok for one's own one-shot command-line, but not for an installable utility. Below is a more efficient implementation, that produces equivalent result at much lower cost. Though both approaches are very cheap on modern computers, there is no never a need to invoke more processes than necessary...
Code: | #!/bin/sh
# $Id: external-ip.sh,v 1.1 2010/08/05 12:57:41 nanard Exp $
# (c) 2010 Reuben Hawkins
exec upnpc "$@" -s | exec sed -e "s/^ExternalIPAddress = //" -e t -e d |
Using exec(1) allows subshell-process(es) to bypass fork(2)-ing in order to execute the command... Some sh-implementations are smart enough to automatically detect the possibility of such bypassing on their own, but not all.
The "$@" allows to pass other options (such as -u descURL) to the program:
|
|