0

Can someone tell me if what I'm trying to do with this script will work? This is in addition to my other question here: How to pass Bash variables as Python arguments

hostnames=hosts.txt

#sets zone as a string into $zone variable
zone="domain.local"

#stores text file into $ip variable
ip=ip.txt

#creates an array
declare -a ipArray

#puts all the contents of the ip.txt file into the ipArray array
ipArray=(`cat "$ip"`)

#increment variable used to access each value in the ipArray array
i=0

#reads the contents of the hosts.text file
while read -r host; do

  #for each host set the zone with the $zone variable
  #for each host set the record-key to the name of each hostname being passed in via the $host variable defined in while loop
  #for each host set the record-value to the current value of the ipArray array based on the index specified in the $i variable
  sudo dns_cli.py --action=delete --zone=$zone --record-type=A --record-key=$host --record-value=${ipArray[i]} >> dns_delete.log 2>&1

  #increment the $i variable by 1
  i=$((i+1))
done < hosts.txt

I ran some echo tests and this appears to be printing out what I want, but I wanted additional input to see if it'll work. I basically need to supply a hostname and the associated IP address so this was the only way I could think of to loop through each host and substitute the IP with each iteration.

I also don't know if i should be enclosing the ${ipArray[i]} part in quotes. Excuse the ugly code, my experience with Bash is pretty much nonexistent.

Community
  • 1
  • 1
Niag Ntawv
  • 197
  • 1
  • 4
  • 16
  • The code looks OK. Ideally, you should use double quotes around the variable names, but in the context it won't matter unless there might be spaces in the lines from `hosts.txt`. The general rule is 'use double quotes unless you've got a specific reason not to do so'. Other than that, it really isn't clear what you're asking. – Jonathan Leffler Jun 07 '16 at 17:25
  • @JonathanLeffler I just wasn't sure how to pass in different values for the --record-key and --record-value (hostname and IP) simultaneously, but looks like chepner answered that already. Thanks for the advice on the double quotes. – Niag Ntawv Jun 07 '16 at 18:41

1 Answers1

2

There's no real point in reading the values into the array; you are only working with one value from the array at a time. Just read from the host file and the IP file simultaneously:

while read -r host
      read -r ip <&3;  do
  sudo dns_cli.py --action=delete --zone="$zone" --record-type=A --record-key="$host" --record-value="$ip" >> dns_delete.log 2>&1
done < hosts.txt 3< ip.txt >> dns_delete.log 2>&1

This, by the way, will now work in any POSIX shell, not just bash.

I am assuming in your original that hosts.txt and ip.txt had the same number of lines; this code will work slightly differently than yours if that is not true, but neither behavior is likely to be what you want if so.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • thanks, I wasn't sure how to read both simultaneously but if the above does that, that's exactly what I needed, thank you! And yes, both files will have the same number of lines and no spaces, each entry is on a newline. Can you explain what <&3 does? – Niag Ntawv Jun 07 '16 at 18:20
  • By default, `read` reads from file descriptor 0 (standard input). `<&3` copies file descriptor 3 to file descriptor 0 for that `read`. Both `read`s inherit their file descriptors from the `while` loop; `< hosts.txt` sets file descriptor 0 to `hosts.txt` for the loop, and `3< ip.txt` sets file descriptor 3 to `ip.txt` for the loop. – chepner Jun 07 '16 at 18:29
  • ahh, makes sense. Probably just forgot to remove it, but I assume since you're appending/redirecting everything to dns_delete.log at the very last line, we can remove it from the line with the python script? I believe the python script has some error checking built-in and I wanted to make sure these get logged – Niag Ntawv Jun 07 '16 at 18:40