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

jas*_*t.h 18 scripts

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

Jer*_*err 17

您需要将sed-i-e参数分开。-ie告诉 sed 创建一个附加了“e”的备份文件。

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

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

sudo sed -i -e "s/NEWUSERNAME/$username/" /etc/apache2/sites-available/$newdomain
Run Code Online (Sandbox Code Playgroud)