I need help with...<various>


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I need help with...<various>
# 15  
Old 08-23-2008
You can have as many commands as you like between the "do" and "done", and of course, they can be new loops with "do" and "done" in them. Not sure where you'd need that, though. You can simply modify the sed script to shuffle things around in a different order if that's how you like them.

Code:
sed "s/.*/IP for $host is & at $nameserver"

The "s/from/to/" command in sed will substitute the stuff in "from" with the stuff in "to" on each line. In this case we are replacing the IP address printed by dig with the longer string. & retrieves the "from" string into the "to" string. Dot star is the regular expression for "everything on this line" (dot means any character, star means any number of times).

Just to show an alternate way of doing it, here's a loop which prints the message piecemeal.

Code:
#!/bin/sh
host=$1
dig +short ns $host |
while read nameserver; do
  echo -n "IP for $host is "
  dig +short @$nameserver $host | tr '\n' ' '  # replace final newline with space
  echo "at $nameserver"
done

Or you can interpolate with backticks

Code:
  echo IP for $host is `dig +short @$nameserver $host` at $nameserver

Backticks AKA grave accents (ASCII 96) capture the output of one command so you can use it in the command line of another command. Notice that those are not regular single quotes. Copy/paste them if you can't find them on your keyboard.

Last edited by era; 08-23-2008 at 09:57 PM.. Reason: Show alternate way of coding it with echos to print the desired message
# 16  
Old 10-01-2008
And here's the answer I wanted with desired output:

host -t ns example.com | while read dom ns server; do dig +short $dom | echo $server "shows an IP of" `dig +short $dom` for $dom; done <enter>

Gives me this output:
server a.iana-servers.net. shows an IP of 208.77.188.166 for example.com
server b.iana-servers.net. shows an IP of 208.77.188.166 for example.com

Thanks era for the assist. Smilie
# 17  
Old 10-01-2008
The pipeline to echo is just discarded; you can take that out.

Code:
host -t ns example.com | while read dom ns server; do
  echo $server "shows an IP of" `dig +short $dom` for $dom;
done

Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question