[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

bash script variables from file

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
nameserver 192.168.1.30
nameserver 192.168.1.29
search domain.com
0
projects
Asked:
projects
1 Solution
 
woolmilkporcCommented:
# 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
0
 
projectsAuthor Commented:
Thank you.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now