使用另一个文件中的数据的 Bash 脚本

使用另一个文件中的数据的 Bash 脚本

我编写了以下脚本,我想用它来轻松添加多个 Postfix 以及其他一些东西。

下面是一个小样本

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

这个脚本有三个要求,$new $ip $domain我想知道如何将它们逐行添加到文件中,然后 bash 这个脚本,一行=脚本的一次完整运行。

例如start包含

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

第一行应运行以下内容

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

依此类推,直到start文件中的所有行都完成

答案1

这是我将用以下内容替换您的脚本:

#!/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

...我所做的就是while在您现有的代码周围包裹一个循环。

如果我可以建议另一项小改动,那就是将所有声明合并echo到一个“此处”文档中,如下所示:

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

如果不是出于性能原因(您可能不在乎),而是因为它更易于阅读。

答案2

你可以这样做:

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

相关内容