使用shell脚本将行附加到/ etc / hosts文件

斯蒂芬·豪威尔斯(Stephen Howells):

我有一个新的Ubuntu 12.04 VPS。我试图编写一个安装脚本来完成整个LAMP安装。我遇到麻烦的地方是在/etc/hosts文件上追加了一行我当前的主机文件如下所示:

127.0.0.1       localhost Venus

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

我希望它看起来像这样:

127.0.0.1       localhost Venus
192.241.xx.xx  venus.example.com venus

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

我已经sed使用append(\a)命令尝试了多种命令。由于某种原因,Ubuntu要么只是hosts在终端中回显文件的内容,要么什么都不做。如何使用bash脚本将第二行正确地注入文件中?

达米安·弗朗索瓦(damienfrancois):

确保使用的-i选项sed

-i[SUFFIX], --in-place[=SUFFIX]
  edit files in place (makes backup if extension supplied)

sed -i "2i192.241.xx.xx  venus.example.com venus" /etc/hosts

除此以外,

echo "192.241.xx.xx  venus.example.com venus" >> /etc/hosts

会在文件末尾附加该行,这可能会按预期工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章