18

I have a script that adds a new user and creates a virtual host for the users domain name. The script works great with one exception... in /etc/apache2/sites-available/ all of my virtual host files have two copies, one with an e and one without.

I believe my issue lies when I use the SED command. Can I get a second opinion?

Here is the script:

# 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
    Sorry, after typing that I realized my mistake, when using the SED command I had the -i option to edit the file "inline" without creating the copy. For some reason I added the e command as well, which is supposed to add the script. I changed the -ie to -i and the script works as needed. I cannot answer my own question yet so I added a comment. – jason.dot.h Jul 4 '11 at 17:44
17

You need separate -i and -e arguments to sed. -ie is telling sed to create a backup file with an 'e' appended.

manual entry for <code>-i</code>

To fix, just replace the sed invocation above to:

sudo sed -i -e "s/NEWUSERNAME/$username/" /etc/apache2/sites-available/$newdomain
  • Although you've got a comment with this, I've adding the details as an answer for completeness. – Jeremy Kerr Jul 5 '11 at 2:40
  • When I first read this answer I understood what it was saying but didn't immediately grasp why the e was being taken as the suffix. I then checked the manual page and realized the i takes an optional argument. It makes this answer obvious in retrospect but it took me a second to understand the why (hence the image add). – Chris Schmitz Mar 8 '19 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.