Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to supply either '-name' or '-sname' when starting an Erlang VM depending on whether the given string is a fully qualified name (FQDN) or not. What would be the quickest and environment-independent way of validating that? I am more interested in using some bash command (like 'getent' or 'nslookup' or others) rather than a regex expression. Best if it works in Ubuntu, FreeBSD and Solaris without changes and can be used in bash's 'if' easily.

share|improve this question
Pretty much any string can be a valid fqdn provided it has a dot in it or at the end, so a lookup is the only way to be certain. – Paul May 6 '14 at 14:04
You mean nslookup? That's what I mean. I want to make a lookup to be certain. I am just not sure if 'nslookup' is the way to go or maybe 'host' instead or something else? – Amiramix May 6 '14 at 14:06
it seems like it might be a better idea to target a python or perl method instead of shelling a utility. shells change all the time, but a system with python3 will always run the same method correctly, regardless of what platform its installed on, and how that platform is configured. – Frank Thomas May 6 '14 at 15:00
I just need to use it in an existing bash script when starting an Erlang node. Adding dependency on Python or Perl would be an overkill in my opinion. – Amiramix May 6 '14 at 16:45
up vote 1 down vote accepted

Host seems to work:

jalderman@mba:/tmp$ cat

for h in "bert" "ernie" ""
    host $h 2>&1 > /dev/null
    if [ $? -eq 0 ]
        echo "$h is a FQDN"
        echo "$h is not a FQDN"

jalderman@mba:/tmp$ ./ 
bert is not a FQDN
ernie is not a FQDN is a FQDN
share|improve this answer
Looks like host could be replaced with nslookup and the script would work pretty much the same. Thanks. – Amiramix May 7 '14 at 10:13

Unfortunately, I can not comment, but this is in fact a comment to the accepted answer: The solution does not check if the provided argument is really a FQDN. Simple host names that can actually be resolved will also qualify (simplest example: "localhost"). So perhaps the title of the question should be changed accordingly, if the question is not for a fqdn but simply for a resolving name. To actually add to the solution: If a fqdn is really required, you could simply check for a dot in the given name, like this:

is_fqdn() {
  [[ $hostname == *"."* ]] || return 1
  host $hostname > /dev/null 2>&1 || return 1

This will still allow for "localhost.", but thats technically a fqdn, anyways.

share|improve this answer
Good point. This is definitely an improvement over the previous answer. This answer could be the accepted answer if only the previous answer didn't help me to resolve my problem earlier :) – Amiramix Mar 9 at 11:05
I like this answer also but should't it check for more than one "."? [[ $hostname == *"."*"."* ]] – johnnyB May 11 at 20:41
No. Ensuring a single dot is present makes sure no plain hostname is provided (except for the already mentioned "localhost.", which is technically the fqdn for localhost, as localhost does not belong to any domain (except for the root label ".")). Any string containing at least one dot is potentially a valid fqdn. The usage of the "host" command makes sure the string resolves to an ip address, which means it is actually a fqdn. – Nicolai Ehemann May 17 at 8:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .