Issue
I have a specific line in the hosts file that is used for my local Wordpress Multisite install, which looks like this:
127.0.0.1 localhost domain.dev site1.domain.dev site2.domain.dev
I'm writing a Shell script that I can run to create a new individual site on my Multisite install straight from Terminal. I need to be able to add a new record to my /etc/hosts file whenever I run this command. In the Shell file, I'm using $site_addr
as my variable for the site name. You then need to add .domain.dev to the end of this. An example I've done (which didn't work) using sed:
sed -i'.bak' '7i7 $site_addr.domain.dev' /etc/hosts
Yhe line from my hosts file (above) that I need to edit is line 7, hence the 7i7
in the sed command. I want to add the new record at the end of the line. I also want to create a backup hosts file in case anything goes wrong.
Thanks for any help guys, hope i've explained it well enough :)
Solution
if this is the line 7
sed -i '.bak' "7 s/$/ ${site_addr}.domain.dev/" /etc/hosts
but you can use the pattern matching (a bit safer) with
sed -i '.bak' "/127\.0\.0\.1/ s/$/ ${site_addr}.domain.dev/" /etc/hosts
i recommend to also first check if the domain is not already in
Answered By - NeronLeVelu Answer Checked By - Robin (WPSolving Admin)