使用 sed 的脚本在输出文件中添加“e”

使用 sed 的脚本在输出文件中添加“e”

我有一个脚本,用于添加新用户并为用户域名创建虚拟主机。该脚本运行良好,但有一个例外... 在 /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.."

答案1

您需要单独的-i-e参数给 sed。-ie告诉 sed 创建一个带有附加“e”的备份文件。

<code>-i</code> 的手动输入

要修复此问题,只需将上面的 sed 调用替换为:

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

相关内容