sed is a very powerful tool. Small than awk and grep and much more powerful. Some would also say it's harder to use, but I like it :)

Whilst putting a mini resolvconf into dhcpcd, I use very similar code to what I already had. But, I ran into a wall - dhcpcd needs to work when /usr is not mounted as it may be network mounted later. On Gentoo and most Linux distros, sed is found in /bin. However, on NetBSD it's in /usr/bin.

Oh what is a man to do :?

Luckily, we can fall back to shell for simple sed usage. Here's a simple snippet

key_get_value()
{
       local key="$1" value= x= line=
       shift
       if [$# -eq 0 ](); then
               while read line; do
                       case "${line}" in
                       "${key}"*) echo "${line##${key}}";;
                       esac
               done
       else
               for x; do
                       while read line; do
                               case "${line}" in
                               "${key}"*) echo "${line##${key}}";;
                               esac
                       done < "${x}"
               done
       fi
}

Old call

sed -n -e 's/^nameserver //p' /etc/resolv.conf

New call

key_get_value "nameserver " /etc/resolv.conf

According to the time command on dash and bash shells this is ever so slightly faster when doing resolvconf -u. This is probably because we aren't forking an external command. Also, we're working on small files - this probably sucks hard for big ones.

Anyway, this has made it into openresolv-1.6 which now works in the root of a BSD system without /usr mounted :)

Add a comment

Next Post Previous Post