Help to correct dnsscript.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help to correct dnsscript.
# 1  
Old 08-13-2012
Question 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.

Code:
#!/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)

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

The 25th line is
Code:
FQDN=`nslookup $ip | grep Name | awk '{print $2}'`

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

Many Thanks,
Praveen


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-13-2012 at 05:43 AM.. Reason: code tags
# 2  
Old 08-13-2012
As side note: Lots of cats 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:
Code:
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.

Last edited by zaxxon; 08-13-2012 at 05:55 AM.. Reason: corrected info
# 3  
Old 08-13-2012
Resolved the error

Hi Zaxxon,
Thank you so much for helping/guiding me. Now the script is working fine.
I made following changes as you suggested..
Code:
cat $1 | tr "[:upper:]" "[:lower:]" | while read ip name

and I changed the below logic ..
Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Get correct mail id

Hi Team, I have below data , could you please help to get correct mail id in oracle only. bhatt,rabi rabi.bhatt@n.com, test, mishra test.mishra@n.com, skype, amit skype.amit@n.com, output like :rabi.bhatt@n.com test.mishra@n.com skype.amit@n.com Please use CODE tags as required... (6 Replies)
Discussion started by: Jewel
6 Replies

2. Shell Programming and Scripting

Can some one correct this script

Hi, I tried writing a script and there was a problem with SFTP part can some one correct where is is the mistake Enveronment file #!/bin/bash export HOST_NAME=<> export USER_NAME=<> export PASSWORD=<> export SOURCE_PATH=/u03/informatica/current/server/infa_shared/TgtFiles/mfg export... (4 Replies)
Discussion started by: spradeep86
4 Replies

3. UNIX for Advanced & Expert Users

I was trying this command...am I going correct? other there is better way

I was trying to copy all debs from apt cache to some storage location and I was taking this approach... /var/cache/apt/archives# ls -1 | grep -v jdownloader | fgrep .deb | xargs cp /media/eshant/L-STORE/Softwares/openjdk/an error bla_bla.deb is a not directory stalled me Suggestions please... (9 Replies)
Discussion started by: ezee
9 Replies

4. Shell Programming and Scripting

Please Correct My script

############### #filename.sh ############### CUREENT_DATE=02 log_file_path="$CUREENT_DATE"-"${0##%*/}`|cut -d "." -f1|awk -F "/" '{print $NF}'`"".log" echo $log_file_path ################ #output required 02-filename.log (6 Replies)
Discussion started by: mohitmehral
6 Replies

5. Solaris

in correct drive name

I am new to solaris and I replaced a faulty tape drive sun DLT7000 But, I am getting the follwoing error when system reboots ltid deamon error drive index 1 is not correct, drive name /dev/rmt/2cbn is incorrect no such file or directory. I have two drives the other one is /dev/rmt/0cbn,... (8 Replies)
Discussion started by: latif1958
8 Replies

6. Shell Programming and Scripting

Please correct this

I have input file like this Input file: ABC|abc_etc_passwd XYZ|XYZ_etc_passwd zXY|XYZ_etc_passwd IJK|test_etc_passwd KLM|test_etc_passwd i want to do following in a loop. grep 'ABC' *abc_etc_passwd* grep 'XYZ' *XYZ_etc_passwd* grep 'ZXY' *ZXY_etc_passwd* i have tried this for i... (2 Replies)
Discussion started by: pinnacle
2 Replies

7. Shell Programming and Scripting

can u please confirme the correct

st1=hello st2=world if && || ] ] ..... .... fi (5 Replies)
Discussion started by: mail2sant
5 Replies

8. Shell Programming and Scripting

Count not correct

the count is off ... man ... help please. The Code open (FILE1, "xy1.TXT") or die "$0: Could not open SOURCEFILE.TXT: $!\n"; open (FILE2, "xy2.TXT") or die "$0: Could not open RESULTFILE.TXT: $!\n"; chomp(my @strings = <FILE2>); while (1) { foreach $pattern (<FILE1>) { ... (3 Replies)
Discussion started by: popeye
3 Replies

9. Shell Programming and Scripting

Is the script correct ???

Dear Collegues is the below given is correct ? #!/usr/bin/perl $a = @ARGV; while ($a = @ARGV) { exec "./jagan ../dat/ml_in @ARGV"; } Jagan (0 Replies)
Discussion started by: jaganadh
0 Replies
Login or Register to Ask a Question