The $? variable contains the exit status of the last command. I just ran two commands:
host www.tamu.edu
and
host blah.blah.blah
Both got the same exit status: 0.
www.tamu.edu is a valid url, and the "blah" is obviously bad. Similarly, for dotted notation, both good and bad IP addresses received 0 as return status from the host command.
So, your script will need to be more intelligent. You need to grep the results for specific strings. When host could not resolve, it printed something like:
Host blah.blah.blah not found: 3(NXDOMAIN)
If you grep for "not found:", then that will tell you if the address resolved. In other words:
Code:
#!/bin/bash
# Store the line of text that contains 'not found:'
# from the host command's output
host_resolved=`host $1 | grep "not found:"`
# Did the grep give us a NULL/empty string
if [ -z "$host_resolved" ] ; then
echo "Host resolved successfully"
exit 0
fi
# If we got here, that means we didn't exit, which
# means the address didn't resolve.
exit 1