pkgsrc-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: pkg_alternatives, Darwin and locale





Joerg Sonnenberger wrote:
On Mon, Jul 21, 2008 at 05:02:08PM -0400, Louis Guillaume wrote:

+found=$(awk '!/^[      ]*#/{
+ f = "if [ -x " $1 " ] ; then echo " $0 " ;fi " | getline found + if ( f == 1 ) {
+    print found
+    exit
+  }
+}' $files )
+

Better, but still better is to use a while loop for the outer part.
The problem with "while read foo" in shell is that it will do a system
call per byte. If you can move all the other processing inside awk,
that is preferable. The test builtin is less expensive than the reading
though, so
awk ... | while read prog alternative; do
        if [ -x "$prog" ]; then
                echo $alternative
                break
        fi
done

or so is most readable and involves the smallest number of forks.

Joerg

So how's about something like ...

awk '!/^[ \t]*#/{ print $1,$0 }' $files | while read prog alt ;do
  [ -x "${prog}" ] && exec ${alt} "${@}"
done

echo "${progname}: no alternatives found" >&2
exit 1




Home | Main Index | Thread Index | Old Index