问题描述
我有一个脚本,可以添加新用户并为用户域名创建虚拟主机。该脚本工作得很好,但有一个例外……在 /etc/apache2/sites-available/中,我的所有虚拟主机文件都有两份副本,一份带有 e,一份没有。
我相信我的问题在于使用 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.."
最佳回答
您需要为 sed 使用单独的 -i
和 -e
参数。 -ie
告诉 sed 创建一个附加 ‘e’ 的备份文件。
要修复此问题,只需将上面的 sed 调用替换为:
sudo sed -i -e "s/NEWUSERNAME/$username/" /etc/apache2/sites-available/$newdomain