bash script variables from file

Posted on 2014-07-11
Last Modified: 2014-07-11
How can I read the dns servers from /etc/resolv.conf so that I can use them as variables??

# get nameservers
for ((i=1;; i++)); do
read "d$i" || break;
done < /etc/resolv.conf

echo "$d2"  | awk '{ print $2 }')
echo "$d3"  | awk '{ print $2 }')

I need d2 and d3 turned into DNS1 and DNS2 respectively without the other text.

The files contents are;

# Interface lan
Question by:projects
    LVL 68

    Accepted Solution

    # get nameservers
    for ((i=1;; i++)); do
    read "d$i" || break;
    done < /etc/resolv.conf

    DNS1=$(awk '{ print $2 }' <<< "$d2")
    DNS2=$(awk '{ print $2 }' <<< "$d3")

    echo $DNS1
    echo $DNS2

    Author Comment

    Thank you.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
    Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now