Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have worked on the following script which I want to use to easily add multiple Postfix as well as a few other things.

Below is a small sample

postmulti -e init
postmulti -I postfix-$new -e create
cd /etc/postfix-$new
rm -rf main.cf
wget http://www.********.com/*******/main.zip
unzip main.zip
mv main main.cf
echo -e "queue_directory = /var/spool/postfix-$new" >> /etc/postfix-$new/main.cf
echo -e "data_directory = /var/lib/postfix-$new" >> /etc/postfix-$new/main.cf
echo -e "multi_instance_name = postfix-$new" >> /etc/postfix-$new/main.cf
echo -e "mydomain = $domain" >> /etc/postfix-$new/main.cf
echo -e "myhostname = host1.$domain" >> /etc/postfix-$new/main.cf
echo -e "smtp_bind_address = $ip" >> /etc/postfix-$new/main.cf
sed -i "s/oldip/$ip/g" /etc/postfix-$new/main.cf

mv /etc/opendkim/keys/$domain/default.private /etc/opendkim/keys/$domain/default
echo -e "/ndefault._domainkey.$domain $domain:default:/etc/opendkim/keys/$domain/default" >> /etc/opendkim/KeyTable
echo -e "/n*@$domain default._domainkey.$domain" >> /etc/opendkim/SigningTable
sed -i "s/cyberciti.com/$domain/g" /etc/postfix-$new/main.cf

There are three requirements for this script $new $ip $domain I want to know how I can add them into a file line by line and then bash this script, one line = one full run of the script.

For example start contains

new1, 1.1.1.1, myweb.com
new2, 2.2.2.2, myweb2.com

The first line should run the following

postmulti -e init
 postmulti -I postfix-new1 -e create
 cd /etc/postfix-new1
 rm -rf main.cf
 wget http://www.********.com/*******/main.zip
 unzip main.zip
 mv main main.cf
 echo -e "queue_directory = /var/spool/postfix-new1" >> /etc/postfix-new1/main.cf
 echo -e "data_directory = /var/lib/postfix-new1" >> /etc/postfix-new1/main.cf
 echo -e "multi_instance_name = postfix-new1" >> /etc/postfix-new1/main.cf
 echo -e "mydomain = myweb.com" >> /etc/postfix-new1/main.cf
 echo -e "myhostname = host1.myweb.com" >> /etc/postfix-new1/main.cf
 echo -e "smtp_bind_address = 1.1.1.1" >> /etc/postfix-new1/main.cf
 sed -i "s/oldip/1.1.1.1/g" /etc/postfix-new1/main.cf

 mv /etc/opendkim/keys/myweb.com/default.private /etc/opendkim/keys/myweb.com/default
 echo -e "/ndefault._domainkey.myweb.com myweb.com:default:/etc/opendkim/keys/myweb.com/default" >> /etc/opendkim/KeyTable
 echo -e "/n*@myweb.com default._domainkey.myweb.com" >> /etc/opendkim/SigningTable
sed -i "s/cyberciti.com/$myweb.com/g" /etc/postfix-new1/main.cf

and so on until all lines in the start file are complete

share|improve this question
    
If your "start" file can be made space-delimited, you could just wrap your existing script with: while read new ip domain; do ... existing script ... done < start –  Jeff Schaller Jul 3 at 1:05
    
Yes it can be spaced rather than comma, anything really. How can I do that? Can you post the answer please? –  Teddy291 Jul 3 at 1:18

2 Answers 2

up vote 0 down vote accepted

Here is what I would replace your script with:

#!/bin/bash
while read new ip domain
do
  postmulti -e init
  postmulti -I postfix-$new -e create
  cd /etc/postfix-$new
  rm -rf main.cf
  wget http://www.********.com/*******/main.zip
  unzip main.zip
  mv main main.cf
  echo -e "queue_directory = /var/spool/postfix-$new" >> /etc/postfix-$new/main.cf
  echo -e "data_directory = /var/lib/postfix-$new" >> /etc/postfix-$new/main.cf
  echo -e "multi_instance_name = postfix-$new" >> /etc/postfix-$new/main.cf
  echo -e "mydomain = $domain" >> /etc/postfix-$new/main.cf
  echo -e "myhostname = host1.$domain" >> /etc/postfix-$new/main.cf
  echo -e "smtp_bind_address = $ip" >> /etc/postfix-$new/main.cf
  sed -i "s/oldip/$ip/g" /etc/postfix-$new/main.cf

  mv /etc/opendkim/keys/$domain/default.private /etc/opendkim/keys/$domain/default
  echo -e "/ndefault._domainkey.$domain $domain:default:/etc/opendkim/keys/$domain/default" >> /etc/opendkim/KeyTable
  echo -e "/n*@$domain default._domainkey.$domain" >> /etc/opendkim/SigningTable
  sed -i "s/cyberciti.com/$domain/g" /etc/postfix-$new/main.cf

done < start.txt

... all I did was wrap a while loop around your existing code.

If I can suggest one other minor change, it would be to consolidate all of your echo statements into one "here" document, like this:

cat >> /etc/postfix-$new/main.cf << EOF
queue_directory = /var/spool/postfix-$new
data_directory = /var/lib/postfix-$new
...
EOF

If not for performance reasons (you may not care), but because it's easier to read.

share|improve this answer
    
This code works under the assumption from the original comments that the input start.txt file is space-separated. –  Jeff Schaller Jul 3 at 11:45
    
Thank you this works! The only problem is with "echo 'abuse@$domain [email protected]' >> /etc/postfix/virtual" - That prints as abuse@$domain rather than the domain. Would doing what you suggested above (consolidate all of your echo statements) fix this? –  Teddy291 Jul 3 at 14:03
    
The single quote marks prevent variable expansion, so change them to double quotes or remove them altogether. –  Jeff Schaller Jul 3 at 20:00

You can do something like:

while read line;
do
new1=$(echo $line | cut -d"," -f1)
ip=$(echo $line | cut -d"," -f2)
domain=$(echo $line | cut -d"," -f3)
<Your bash script here with above three variables>
done < file.txt

I hope it helps ..

share|improve this answer
    
file.txt should contain "new1, 1.1.1.1, myweb.com" ? –  Teddy291 Jul 3 at 2:13
    
Not necessary.. As long as the ip, domain and new1 field are separated by comma, any data can be used. But, be certain that no comma should appear other than those three. –  UbaId Ashraf Jul 3 at 2:22
    
This didn't work for me –  Teddy291 Jul 3 at 10:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.