Help to correct dnsscript.

Hello Brains,

I was trying to develop a script that would do nslookup using both name and ip of server and format the output and store in an output file. Please find the script below.

#!/usr/bin/ksh
cat $1 | tr "[:upper:]" "[:lower:]" |
while read ip name
 do
  ERROR="$(nslookup $ip | grep can't | awk -F" " '{print $5}' > /dev/null 2>/dev/null;echo $?)"
   if [[ $ERROR -eq 0 ]]
    then
         FQDN=`nslookup $name.symc | grep Name | awk '{print $2}'`
         echo $ip $FQDN $name >> $1.hosts
    else
         FQDN=`nslookup $ip | grep Name | awk '{print $2}'`
         echo $ip $FQDN $name >> $1.hosts
   fi
 done

I would be running the script by providing filename as argument. The content of filename would be in format <ip address> <host name>.
While I execute the script, it throws error... (pfb)

# ./dnsscript test
./dnsscript: syntax error at line 25 : `'' unmatched

The 25th line is

FQDN=`nslookup $ip | grep Name | awk '{print $2}'`

Could you please help me to get this script running perfectly ?

Many Thanks,
Praveen

As side note: Lots of cat s and using grep together with awk while awk could do it both.
Also I do wonder why don't get an error at line 1 already where you end it with a pipe | and continue in next line, missing a \ to continue in line 2.

To your problem:

ERROR="$(nslookup $ip | grep can't | awk -F" " '{print $5}' > /dev/null 2>/dev/null;echo $?)"

As the messages says, you didn't escape/quote correct. "can't" is a string, but the single ' is a special character. Put a backslash in front of it to escape it.

Hi Zaxxon,
Thank you so much for helping/guiding me. Now the script is working fine.
I made following changes as you suggested..

cat $1 | tr "[:upper:]" "[:lower:]" | while read ip name

and I changed the below logic ..

ERROR="$(nslookup $ip | grep can't | awk -F" " '{print $5}' > /dev/null 2>/dev/null;echo $?)"
if [[ $ERROR -eq 0 ]]

As you said, I understood the difference of grep can't and grep "can't"

Many Thanks,
Praveen P