Check if a variable contains a domain name in a set of TLDs

bash, shell

Solution

You can use this `egrep`:

egrep -q "\.(com|es|de)$"

This will return 0 (true) if given input is ending with `.com` OR `.es` OR `.de`

EDIT: Using it with an array of allowed domains:

domain=( "es" "com" "de" )
str=$(printf "|%s" ${domain[@]})
str="${str:1}"

echo "abc.test.com"|egrep "\.(${str})$"
abc.test.com

echo "abc.test.org"|egrep "\.(${str})$"

Problem

I have a variable name which contains name of DNS record. I want to check for certain conditions, for example if it ends with `.com` or `.es`. Right now I am using ``` name="test.com" namecheck=`echo $name | grep -w "^[A-Za-z]*..com"` ``` but it only checks the `com` and ignores the `.` also is it possible to check it against series of value stored in array like ``` domain=[ ".es" ".com" ".de"] ```

Original source