Dig match

Hi,

I am testing some code to match a grep to see if one of the dns server exists but it does not seem to match:

ERROR:

CRITICAL: google.com DNS : ns3.google.com NOT found

CODE:

if [`dig +short NS google.com | grep ns3.google.com| wc -l` -eq 1 ]; then

 echo "OK: google.com DNS : ns3.google.com exists"
 
else

 echo "CRITICAL: google.com DNS : ns3.google.com NOT found"
fi

COMMAND:

dig +short NS google.com

OUTPUT:

ns3.google.com.
ns2.google.com.
ns4.google.com.
ns1.google.com.

Thanks,

Heya

For some reason this machine has no dig available, so the following is untesteted.
Try:

SEARCH="ns3.google.com"
RESULT=$(dig +short NS google.com | grep "$SEARCH")
[ "$SEARCH" = "$RESULT" ] && \
    echo "OK: google.com DNS : ns3.google.com exists" || \
    echo "CRITICAL: google.com DNS : ns3.google.com NOT found"

hth

@dmccabe: Your script thorws a different error message than you describe: [1: command not found
This is because there is no space after the square bracket:

if [ `dig +short NS google.com | grep ns3.google.com| wc -l` -eq 1 ]; then

Damn!

Thank you so much!

The condition could be done as just:

$ if [[ -n $(dig ns3.google.com +short) ]] ; then ... fi

Thanks Aia!