Help with bash script to get continual input

I need a script to take input in the form of a server name, perform the nslookup command, output only the actual IP server address (not the DNS server) and print the output. I have part of the script but need help on the rest. Thanks.

______

#!/usr/bin/bash

echo “Enter a ServerName.\n”
read serverName
nsLook = $(echo nslookup $serverName | egrep -o “Non*" | # this is the part where I need help with getting the last IP address
print nsLook

echo “Do you have another?\n”
case:
    yes)
        next;;    # this is the part where I need help
    no)
        break;;
esac
Tech_20Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jan SpringerCommented:
#!/bin/bash

function get_nameservers() {
      read -p "Enter a Server Name: " serverName
      nsLook=`nslookup $serverName`
      server_addy=`echo $nsLook | cut -d " " -f2`
      echo $server_addy
      get_nameservers
}

get_nameservers
0
Jan SpringerCommented:
Though unless you're configured to use multiple DNS servers, the answer back should always be the same.
0
Jan SpringerCommented:
I misread your question and have changed one of the lines:


#!/bin/bash

function get_nameservers() {
      read -p "Enter a Server Name: " serverName
      nsLook=`nslookup $serverName`
      server_addy=`echo $nsLook | cut -d ":" -f6`
      echo $server_addy
      get_nameservers
}

get_nameservers
0
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

Tech_20Author Commented:
Hello Jan,

Thanks for the quick response. This extracted only the nameserver at first. Then after trying it again (and double-checking the terminal session to confirm), it began to work. That buggy issue may have to do with the version of Terminal (OS X El Capitan) I am using but it's currently working.

I have another question regarding an event where there is no server found. Instead of a lengthy error in the form of all of the names of the files in the working directory, is there a possible if/then statement to use in that code block to return "N/A" if the variable $server_addy has no value?
0
Jan SpringerCommented:
This?

#!/bin/bash

function get_nameservers() {
      read -p "Enter a Server Name: " serverName
      nsLook=`nslookup $serverName`
      server_addy=`echo $nsLook | cut -d ":" -f6`
      if [ -z "$server_addy" ]; then
      echo "Server IP not found, possible invalid domain name"
      else
      echo $server_addy
      fi
      get_nameservers
}

get_nameservers
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tech_20Author Commented:
That works! Thanks, Jan!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Bash

From novice to tech pro — start learning today.