sedを使用するスクリプトは、出力ファイルに「e」を追加します


18

新しいユーザーを追加し、ユーザーのドメイン名の仮想ホストを作成するスクリプトがあります。このスクリプトは、1つの例外を除いて問題なく機能します。/etc/apache2/sites-available/にあるすべての仮想ホストファイルには、e付きとなしの2つのコピーがあります。

SEDコマンドを使用すると問題が発生すると考えています。セカンドオピニオンを取得できますか?

スクリプトは次のとおりです。

# set the working directory
dir=$(pwd)

# request the new domain name
printf "Enter the new domain name, without the www (i.e newdomain.com):\n"
read newdomain

# request the new username
printf "Enter the new username (i.e newusername):\n"
read username

# create the new user
sudo adduser $username

# copy the virtual host to sites-available
sudo cp /templates/domain-vhost /etc/apache2/sites-available/$newdomain

# echo results
echo "Successfully created the virtual host file at: /etc/apache2/sites-available/$newdomain.."

# change the domain name in the virtual host file
sudo sed -ie "s/NEWDOMAINNAME/$newdomain/" /etc/apache2/sites-available/$newdomain

# echo results
echo "Modified the virtual host to reflect the new domain: $newdomain.."

# change the directory path in the virtual host
sudo sed -ie "s/NEWUSERNAME/$username/" /etc/apache2/sites-available/$newdomain

# echo results
echo "Successfully modified the new virtual host file.."

# enable the site with apache
cd /etc/apache2/sites-available/
sudo a2ensite $newdomain

# echo results
echo "Successfully enabled the $newdomain.."

# change to previous working directory
cd $dir

# reload apache
sudo /etc/init.d/apache2 reload

# notify user of action
echo "Finished creating the new domain, $newdomain, and restarted Apache.."

3
申し訳ありませんが、タイプミスの後、SEDコマンドを使用するときに、コピーを作成せずに「インライン」でファイルを編集する-iオプションがありました。何らかの理由で、スクリプトを追加することになっているeコマンドも追加しました。-ieを-iに変更すると、スクリプトは必要に応じて機能します。私はまだ自分の質問に答えることができないので、コメントを追加しました。
jason.dot.h

回答:


17

あなたは分ける必要が-i-esedの引数に。-ie「e」が追加されたバックアップファイルを作成するようにsedに指示しています。

<code> -i </ code>の手動入力

修正するには、上記のsed呼び出しを次のように置き換えます。

sudo sed -i -e "s/NEWUSERNAME/$username/" /etc/apache2/sites-available/$newdomain

これについてコメントがありますが、完全性の答えとして詳細を追加します。
ジェレミーカー

最初にこの答えを読んだとき、私はそれが言っていることを理解しましたが、なぜeが接尾辞として取られたのかすぐには分かりませんでした。次に、マニュアルページを確認し、iがオプションの引数を取ることを認識しました。振り返ってみると、この答えが明らかになりますが、その理由を理解するのに少し時間がかかりました(したがって、画像が追加されます)。
クリスシュミッツ
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.