For the below scripts, domains.txt
is a text document that contains two lines:
google.com
adsfadsfad.net
I spent about two hours trying to troubleshoot why I couldn't get my grep
within a if [[ ]]
test to return what I was looking for:
#!/bin/bash
declare -a domain
while read domain; do
if [[ $(whois -H $domain | grep -i 'No match for "$domain"') ]]; then
echo "$domain is expired"
else
echo "exists"
fi
done < domains.txt
The above script would constantly indicate that both domains exist:
[root@localhost ~]# ./check-dns.sh
exists
exists
I confirmed that the following two tests worked perfectly fine from the shell:
#[root@localhost ~]# if [[ $(whois -H adsfadsfad.net | grep -i 'No match for "adsfadsfad.net"') ]]; then echo "true"; else echo "false"; fi
#true
#[root@localhost ~]# if [[ $(whois -H google.com | grep -i 'No match for "google.com"') ]]; then echo "true"; else echo "false"; fi
#false
I then confirmed that if I edited the script by removing the variable and replacing it with adsfadsfad.net, it produced the result I was looking for:
{snip}
if [[ $(whois -H $domain | grep -i 'No match for adsfadsfad.net') ]]; then
{snip}
adsfadsfad.net is expired
Finally, I settled on just running this test, which produces the correct results:
if [[ $(whois -H $domain | grep -i 'No match for') ]]; then
But my question is, why did the variable $domain
break my script?