views:

291

answers:

2

Hi people,

I have a text file, a.txt. Contents are:

$ cat a.txt
microsoft.com
google.com
ibm.com

I'm trying to run a host command on each line to get the IP address. Here is my script:

#!/bin/sh
for i in `cat a.txt`
do
echo $i
host $i
done

When i run it I get this:

$ ./a.sh
microsoft.com
Host microsoft.com\013 not found: 3(NXDOMAIN)
google.com
Host google.com\013 not found: 3(NXDOMAIN)
ibm.com
Host ibm.com\013 not found: 3(NXDOMAIN)

However if in edit my script to explicitly specify the host :

#!/bin/sh
host microsoft.com
host google.com
host ibm.com

It works.

Do you know I get "013 not found: 3(NXDOMAIN)" ?

Thanks Chris

+2  A: 

You've got some funny characters in your file (at the end of each line). Do:

od -xcb a.txt

and post the results here. It's possible the file came from another system (DOS/Windows) although \013 is the VT character rather than CR.

When I do the same with files created from scratch, I get:

pax@pax-desktop:~$ . ./a.sh
microsoft.com
microsoft.com has address 207.46.197.32
microsoft.com has address 207.46.232.182
microsoft.com mail is handled by 10 mail.messaging.microsoft.com.
google.com
google.com has address 74.125.45.100
google.com has address 74.125.67.100
google.com has address 74.125.127.100
google.com mail is handled by 10 smtp2.google.com.
google.com mail is handled by 10 smtp3.google.com.
google.com mail is handled by 10 smtp4.google.com.
google.com mail is handled by 10 smtp1.google.com.
ibm.com
ibm.com has address 129.42.16.103
ibm.com has address 129.42.17.103
ibm.com has address 129.42.18.103
ibm.com mail is handled by 10 e4.ny.us.ibm.com.
ibm.com mail is handled by 10 e5.ny.us.ibm.com.
ibm.com mail is handled by 10 e6.ny.us.ibm.com.
ibm.com mail is handled by 10 e31.co.us.ibm.com.
ibm.com mail is handled by 10 e32.co.us.ibm.com.
ibm.com mail is handled by 10 e33.co.us.ibm.com.
ibm.com mail is handled by 10 e34.co.us.ibm.com.
ibm.com mail is handled by 10 e35.co.us.ibm.com.
ibm.com mail is handled by 10 e1.ny.us.ibm.com.
ibm.com mail is handled by 10 e2.ny.us.ibm.com.
ibm.com mail is handled by 10 e3.ny.us.ibm.com.

My od output is:

0000000 696d 7263 736f 666f 2e74 6f63 0a6d 6f67
          m   i   c   r   o   s   o   f   t   .   c   o   m  \n   g   o
        155 151 143 162 157 163 157 146 164 056 143 157 155 012 147 157
0000020 676f 656c 632e 6d6f 690a 6d62 632e 6d6f
          o   g   l   e   .   c   o   m  \n   i   b   m   .   c   o   m
        157 147 154 145 056 143 157 155 012 151 142 155 056 143 157 155
0000040 000a
         \n  \0
        012 000
0000041

Have a look at the od output for your file and see if there's anything before the \n characters.

Update:

From your comment, it appears your file was created under DOS/Windows since it has a CR/LF line ending. Use this as the script to strip out CR charcters first:

#!/bin/sh
for j in `cat a.txt`
do
    i=`echo $j | sed 's/\r//g'`
    echo $i
    host $i
done
paxdiablo
Thank you for your unbelievably quick response.---result---$ od -xcb a.txt0000000 696d 7263 736f 666f 2e74 6f63 0d6d 670a m i c r o s o f t . c o m \r \n g 155 151 143 162 157 163 157 146 164 056 143 157 155 015 012 1470000020 6f6f 6c67 2e65 6f63 0d6d 690a 6d62 632e o o g l e . c o m \r \n i b m . c 157 157 147 154 145 056 143 157 155 015 012 151 142 155 056 1430000040 6d6f 0a0d o m \r \n 157 155 015 0120000044
chrisc
@Pax: 13 in decimal is a CR. 13 in octal (i.e. decimal 11) is a vertical tab.
Brandon E Taylor
sorry i dont know how to wrap the answer with code formatting
chrisc
I knew that @Brandon, but the error message indicated \013 which is usually an octal number. @chrisc, the file was created under DOS/Windows. Use dos2unix or sed 's/\r//' to convert it.
paxdiablo
+1  A: 

\013 indicates that you have a carriage return at the end of your host name. Strip it (e.g. using something like tr -d '\r') before passing the host name to 'host'.

Try changing:

for i in `cat a.txt`

to:

for i in `cat a.txt | tr -d '\r'`
Brandon E Taylor
Yes that worked!! I got the a.txt from grepping an xml file.Thank you very much guys!
chrisc