使用SED在hosts文件在行的末尾添加地址 [英] Add address at end of line in hosts file using sed

查看:452
本文介绍了使用SED在hosts文件在行的末尾添加地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于我的本地字preSS多站点安装hosts文件中的特定行,它看起来像这样:

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

我写,我可以运行建立在我的多站点一个新的个人网站从码头直接安装一个Shell脚本。我需要能够以一个新的记录添加到我的/ etc / hosts文件,每当我运行此命令。在shell文件,我使用 $ site_addr 作为我的网站的名称变量。然后,您需要.domain.dev添加到本月底。我做了一个例子(没有工作)使用sed的:

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

从我的hosts文件中(如上图)YHE行,我需要编辑的7号线,因此在sed命令的 7i7 。我想在该行的末尾添加新记录。我也想在以防出错创建备份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 :)

推荐答案

如果这是第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 recommand to also first check if the domain is not already in

这篇关于使用SED在hosts文件在行的末尾添加地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆