他のファイルのデータを使用する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

このスクリプトには3つの要件があります。$new $ip $domainこれをファイルに1行ずつ追加してから、このスクリプトをbashする方法を知りたいです。 1行=スクリプト全体の実行1回。

例えば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別の小さな変更を提案できる場合は、次のようにすべての宣言を1つの「ここ」文書にマージすることです。

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

パフォーマンス上の理由(おそらく気にしないかもしれませんが)ではなく、読みやすくなりやすいからです。

おすすめ記事