Wednesday, February 13, 2008

grepping for periods and at's

A handy one-line hack:

I needed a way of detecting if a user had entered a domain name correctly within a shell script (or had entered an FQDN, rather than just the domain name) -- thankfully, the power of grep came to my rescue:


if [ ! -z "$(echo $dname | grep -oE '[[:alnum:]+\.\_\-]*\.[[:alnum:]+\.\_\-]*\.[[:alnum:]+\.\_\-]*\.')" ] ; then
echo "Please enter the *domain name* only" >&2
exit 0
fi