データベースとWordPressインスタンス生成スクリプトの短縮

データベースとWordPressインスタンス生成スクリプトの短縮

データベースとWordPressインスタンスを作成し、各ドメインの権限を変更し、サーバーを再起動することを目的とした次のスクリプトをどのように短縮できますか?

${domain}スクリプトの実行時にパラメータとして渡されたドメインを示します。 ${drt}ドキュメントルート(/var/www/html)を表します。

#!/bin/sh
domain="$1"
echo "What's your DB root password?" && read -s dbrp
echo "What's your DB user password?" && read -s dbup

echo "CREATE USER "${domain}"@"localhost" IDENTIFIED BY \"${dbup}\";" | mysql -u root -p"${dbrp}"
echo "CREATE DATABASE ${domain};" | mysql -u root -p"${dbrp}"
echo "GRANT ALL PRIVILEGES ON ${domain}.* TO ${domain}@localhost;" | mysql -u root -p"${dbrp}"

cd ${drt}
curl -L http://wordpress.org/latest.tar.gz | tar -zxv -C ${domain}/
cp ${domain}/wp-config-sample.php ${domain}/wp-config.php
sed -i "s/database_name_here/${domain}"/g ${domain}/wp-config.php
sed -i "s/username_here/${domain}/g" ${domain}/wp-config.php
sed -i "s/password_here/${dbup}/g" ${domain}/wp-config.php

chown -R ${domain}:${domain} ${domain}/* && chmod -R a-x,a=rX,u+w ${domain}/* && systemctl restart nginx.service

2~3行ほど減らすのが目標なのに不可能かもしれません。

私の考えでは:

1)cd ${drt}構文を削除し、${drt}必要な場所に追加し始めました。

2)次のように最初の2つのsedをマージします。

sed -i "s/_name_here/${domain}"/g ${domain}/wp-config.php

短縮(結合)できる他の項目を知っているかどうか疑問に思います。

ベストアンサー1

4行が1つにまとめられました。

cp ${domain}/wp-config-sample.php ${domain}/wp-config.php
sed -i "s/database_name_here/${domain}"/g ${domain}/wp-config.php
sed -i "s/username_here/${domain}/g" ${domain}/wp-config.php
sed -i "s/password_here/${dbup}/g" ${domain}/wp-config.php

ターゲット(下記のコメントで正規表現を編集するように編集)

sed "s/[a-z_]*name_here/${domain}/g;s/password_here/${dbup}/g" ${domain}/wp-config-sample.php > ${domain}/wp-config.php

繰り返しますが、サンプル設定ファイルに「name_here」と一致する他の文字列があるかどうかはわかりません。;1行に複数のsed置換を実行できます。

パスワードを求めるのは良いセキュリティ対策ですが、後でコマンドラインでパスワードを使用すると問題が発生するため、プロセス環境のコマンドラインでパスワードを指定することもできます。

dbrp=mySillySecret dbup=mySecret myShortenedWordpressScript.sh example.com

おすすめ記事