pkgsrc-Users archive

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

Re: pkg_alternatives, Darwin and locale



Christian Biere wrote:
> Joerg Sonnenberger wrote:
> > On Sun, Jul 20, 2008 at 04:26:47AM +0200, Christian Biere wrote:
> > > +cat "${userfile}" __CONF_FILE__ __DB_FILE__ 2>/dev/null | \
> > > +{
> > > +    while read line

> > Can you make this awk please? read is horrible slow for larger files...
 
I noticed that the real performance killer is reading through a pipe.
Using a redirect to read from the regular file directly is faster by
magnitudes (albeit still slower by magnitudes than a non-shell solution):

while read line; do :; done < "$file"

It would be nice if the shells were smart enough to enable line buffered
I/O in this case as it should have no side-effects here.

I've attached a cleaner modified pure shell solution which also reduces
forking.

-- 
Christian
--- wrapper.sh.orig     2008-07-22 00:21:10.000000000 +0200
+++ wrapper.sh  2008-07-22 00:36:17.000000000 +0200
@@ -44,21 +44,22 @@
 else
     userfile=~/.pkg_alternatives/${wrapper}
 fi
-alternatives=$(cat ${userfile} __CONF_FILE__ __DB_FILE__ 2>/dev/null | \
-               grep -v "^#" | tr ' ' '�')
 
-found=
-for a in ${alternatives}; do
-    prog=$(echo ${a} | cut -d '�' -f 1)
-    if [ -x ${prog} ]; then
-        found=$(echo ${a} | tr '�' ' ')
-       break
+for file in "${userfile}" __CONF_FILE__ __DB_FILE_
+do
+    if [ ! -f "$file" ]
+    then continue
     fi
+    while read prog args
+    do
+       case "$prog" in
+       ''|'#*') continue;;
+       esac
+        if [ -x "${prog}" ]
+        then exec "$prog" $args "$@"
+        fi
+    done < "$file"
 done
 
-if [ -z "${found}" ]; then
-    echo "${progname}: no alternatives found" 1>&2
-    exit 1
-fi
-
-exec ${found} "${@}"
+echo "${progname}: no alternatives found" 1>&2
+exit 1


Home | Main Index | Thread Index | Old Index