pkgsrc-Users archive

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

Re: pkg_alternatives, Darwin and locale



On Mon, Jul 21, 2008 at 05:02:08PM -0400, Louis Guillaume wrote:
> --- pkgtools/pkg_alternatives/files/wrapper.sh.orig   2008-07-21 
> 08:17:07.000000000 -0400
> +++ pkgtools/pkg_alternatives/files/wrapper.sh        2008-07-21 
> 14:41:04.000000000 -0400
> -for a in ${alternatives}; do
> -    prog=$(echo ${a} | cut -d '¬' -f 1)
> -    if [ -x ${prog} ]; then
> -        found=$(echo ${a} | tr '¬' ' ')
> -     break
> -    fi

cat + grep + cut == evil

> +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


Home | Main Index | Thread Index | Old Index