I try to use name resolution and get IP address of a web site. I used nslookup command and use 6'th line of its output. Because I noticed that output of nslookup contains IP address(in IPv4) in 6'th line. My command was :
website=<somewebsiteURL>
IP=$(nslookup "$website" | head -n 6 | tail -n 1 | cut -d" " -f2)
Also I tried sed command to reach the same goal and used :
website=<somewebsiteURL>
IP=$(nslookup "$website" | sed -n 6p | cut -d" " -f2)
result was the same, result is unreliable, sometimes worked, sometimes not.
It works correctly but not always. Sometimes it reads the 7'th line, not the 6'th line and fails.
Actually I solved my problem using another approach :
website=<somewebsiteURL>
newIP=$(nslookup "$website" | grep "Address: " | head -n 1 | cut -d" " -f2)
which gave the correct line and IP address everytime(although it can give more than one IP > nslookup can return more than one IP)
Why do the first two codes fail?