Hello,

I need to seperate multiple domains by having and not having A records.

I have created a shell script like below, but it is not working, as the $? is same (0) for both the dig commands that have and not have A records.

*************************

!/bin/bash

for dom in cat /home/xxxx/domainslist.txt
do

result= dig +noall +answer $dom

if [ "$result" != '') ];then

echo $dom >> /home/xxxx/havingA.txt

else

echo $dom >> /home/xxxx/havingNoA.txt

fi

done

**************************

Please help me if there is any other way we can do this. Thank you.

Dani AI

Generated

Good catch, — the root cause is that dig's exit status does not indicate whether an A record was returned, so testing $? alone is unreliable. Also avoid the common for dom in $(cat file) pattern (it splits on whitespace) and watch for variable typos like $domains vs $dom. A robust approach reads the list line-by-line, skips blanks/comments, then checks whether dig produced any non-empty A output.

#!/bin/sh
while IFS= read -r domain || [ -n "$domain" ]; do
  [ -z "${domain#\#}" ] || continue   # skip lines starting with #
  [ -z "$domain" ] && continue        # skip blank lines

  if dig +short A "$domain" | grep -q .; then
    printf '%s\n' "$domain" >> /home/xxxx/havingA.txt
  else
    printf '%s\n' "$domain" >> /home/xxxx/havingNoA.txt
  fi
done < /home/xxxx/domainslist.txt

Notes and troubleshooting

  • dig +short A returns IPv4 addresses (or nothing); use dig +short AAAA to detect IPv6.
  • Quote variables ("$domain") to handle weird names safely.
  • Add +time= and +tries= to dig if you need faster failures for many domains.
  • If processing thousands of domains, parallelize carefully (xargs -P or GNU parallel) but avoid hammering upstream DNS servers — consider small concurrency and short sleeps.
  • If results must be atomic when parallelized, write to temp files then merge, or use flock to avoid race conditions.

This keeps the logic focused on inspecting command output (not exit codes) and handles common edge cases left open in the thread.

There is a small syntax error in my previous code and it is corrected as

if [ "$result" != '' ];then

The issue persists. Please help me if anyone have a better idea of doing this.

:-) I have figured it out.

#!/bin/bash

for dom in cat /home/xxxx/domainslist.txt

do

result=$(dig +noall +answer $domains | awk '{print $5}')

if [ "$result" != '' ];then

echo "$dom" >> /home/xxxx/havingA.txt

else

echo "$dom" >> /home/xxxx/havingNoA.txt

fi

done

This will dig the domains one by one that is listed in the domainslist.txt and will seperate by having Address record and not.

Thanks for everyone who tried. :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.