while read scripting trick

Posted by Peter Burkholder Tue, 13 Mar 2007 00:44:00 GMT

I saw this mentioned on the dc-sage email list but missed the particular example, until Sweth Chandramouli posted the following example for testing whether nameservers in /etc/resolv.conf are actually working:

#!/bin/sh
while read TOKEN IP ; do
   case $TOKEN in nameserver )
      echo "Testing DNS query against $IP: `dig -x 127.0.0.1 @$IP | grep ';; ->>'`" |\
      logger -p local3.info -t check_dns ;;
   esac
done < /etc/resolv.conf

Nice trick, although I’d like to cat the input into while at the top:


cat /etc/resolv.conf |
while ...

Oh yes, I have a new job. Director of System and Network Administration at EchoDitto, which is a topic meriting several blog posts.

Posted in  | Tags , , , ,  | no comments