I have a query.sh script which runs dig commands, performing a set of lookups from different DNS servers, which are currently given in a column in the single input file used (sites.txt).
My goal is to modify this script to use a different input file, dns_servers.txt, to find the DNS servers to iterate through for each query.
I'm not clear on where to start here. What do I need to do to be able to safely nest while read loops?
Current inputs:
query.sh
#!/bin/sh
while read line;
do
set $line
echo "SITE:" $1@$2
/usr/sbin/dig +short -4 @$2 $1
sleep 5
exitStatus=$?
echo "Exit Status: " $exitStatus
done < sites.txt
sites.txt
Current format has a hostname and a DNS server to use for lookups against that hostname.
www.google.com 8.8.4.4
The intent is for the column with the DNS server to be ignored, and the contents of dns_servers.txt to be used instead.
Desired Inputs
dns_servers.txt
10.1.1.1
12.104.1.232
...
set $lineis very, very buggy (look at what happens ifsite.txtcontains a line with*). Use a real array, andread -r -ainstead. Though, for only two items, you don't even need that:while read -r site_name destination_ipwill put what's currently$1insite_nameand what's currently$2indestination_ip.while read name ip;, then drop thesetcommand and use$nameand$ipin place of$1and$2. Also,exitStatusis the exit status ofsleep, notdig; you need to save the value of$?immediately after the command whose status you want.bash, but your shebang is#!/bin/sh. Bash scripts must use#!/bin/bash; the shebang#!/bin/shis for POSIX sh, and any script using it is not guaranteed to have access to features not promised in the POSIX shell specification.