1

I'm having some issues looping until the end of each file is reached.

If i run the code and in each file there is only 1 line it works just fine. But if i include more than 1 line in the files its not working

I believe i'm incorrectly using the FOR loop, any help would be greatly appreciated.

#!/bin/bash
domain="$(cat domains.txt)"
ip="$(cat ips.txt)"
pub_dkim_key="$(cat pub_dkim_key.txt)"
password="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-25};echo;)"

# -- ~~ --  -- ~~ --  -- ~~ --  -- ~~ --  -- ~~ --  -- ~~ --  -- ~~ --  -- ~~ -- #
for domain in $domain
do
# Add domains
v-add-domain admin $domain $ip restart

# Delete DNS Records
v-delete-dns-record admin $domain 16
v-delete-dns-record admin $domain 15

# Add NS A Records
v-add-dns-record admin $domain NS1 A $ip
v-add-dns-record admin $domain NS2 A $ip

# Change localhost to correct domain
v-change-dns-record admin $domain 1 ns1.$domain.
v-change-dns-record admin $domain 2 ns2.$domain.

# Add key1 DKIM
v-add-dns-record admin $domain key1._domainkey TXT "v=DKIM1; k=rsa; p=$pub_dkim_key"

# Add SPF
v-add-dns-record admin $domain @ TXT "v=spf1 mx a ip4:1.1.1.1/24 include:amazonses.com ~all" 

# Add DMARC
v-add-dns-record  admin $domain _dmarc TXT "v=DMARC1; p=none; sp=none; rua=mailto:admin@$domain; ruf=mailto:admin@$domain; rf=afrf; pct=100; ri=86400"

# Add email accounts
v-add-mail-account admin $domain admin $password 1024
v-add-mail-account admin $domain abuse $password 1024
v-add-mail-account admin $domain contact $password 1024
v-add-mail-account admin $domain fbl $password 1024
v-add-mail-account admin $domain hostmaster $password 1024
v-add-mail-account admin $domain noreply $password 1024
v-add-mail-account admin $domain postmaster $password 1024
v-add-mail-account admin $domain reply $password 1024

# Add Email Forward
v-add-mail-account-forward admin $domain admin  all@asdfasdf.com
v-add-mail-account-forward admin $domain abuse  all@asdfasdf.com
v-add-mail-account-forward admin $domain contact  all@asdfasdf.com
v-add-mail-account-forward admin $domain fbl  all@asdfasdf.com
v-add-mail-account-forward admin $domain hostmaster  all@asdfasdf.com
v-add-mail-account-forward admin $domain noreply  all@asdfasdf.com
v-add-mail-account-forward admin $domain postmaster  all@asdfasdf.com
v-add-mail-account-forward admin $domain reply  all@asdfasdf.com

# Add Lets-Encrypt SSL
v-add-letsencrypt-domain admin $domain
done
# -- ~~ --  -- ~~ --  -- ~~ --  -- ~~ --  -- ~~ --  -- ~~ --  -- ~~ --  -- ~~ -- #

EDIT:

Hope this makes things more clear for what i'm trying to Achieve,

Pretty much i want it to loop over the commands using the domains/ips from the specified files until it reaches the end of the file(s)

domains.txt 
    a.com 
    b.com 
    c.com 

ips.txt 
    1.1.1.1 
    2.2.2.2 
    3.3.3.3 

pub_dkim_key.txt 
    MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC 
    MIIEowIBAAKCAQEAgr6rzGUQBoi2IICq+RSCDACf 
    bCb3lZAhn0pmaTnMdWaUZmSDvBdfi8UlEs9LeoUn 


First Run   v-add-domain admin a.com 1.1.1.1 restart 
Second Run  v-add-domain admin b.com 2.2.2.2 restart 
Third Run   v-add-domain admin c.com 3.3.3.3 restart 

First Run   v-change-dns-record admin a.com 1 ns1.a.com. 
First Run   v-change-dns-record admin a.com 2 ns2.a.com. 

Second Run  v-change-dns-record admin a.com 1 ns1.a.com. 
Second Run  v-change-dns-record admin a.com 2 ns2.a.com. 

Third Run   v-change-dns-record admin a.com 1 ns1.a.com. 
Third Run   v-change-dns-record admin a.com 2 ns2.a.com. 

etc
Dennis
  • 21
  • 6
  • `$ip` is going to contain the complete contents of `ips.txt`. Maybe you want something like https://stackoverflow.com/questions/16644645/how-to-read-from-multiple-files-in-one-loop (chepner's answer, specifically)? – Benjamin W. Jul 31 '18 at 15:17
  • ...to give you an answer while I'm working on improving the duplicate list, however: `while read -r domain <&3 && read ip <&4 && read dkim <&5; do echo "Processing ip $ip, domain $domain and dkim key $dkim"; done 3 – Charles Duffy Jul 31 '18 at 17:36

0 Answers0