hosts 文件内容
127.0.0.1 localhost 127.0.1.1 exmaple-some.com # The following lines are desirable for IPv6 capable hosts ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters 使用脚本
add-host() { sed -i "/^# The following/i\\${1}" hosts } add-host '127.0.1.1 a.newdomain.com' add-host '127.0.1.1 b.newdomain.com' 期望结果
127.0.0.1 localhost 127.0.1.1 exmaple-some.com 127.0.1.1 a.newdomain.com 127.0.1.1 b.newdomain.com # The following lines are desirable for IPv6 capable hosts ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters 实际结果
127.0.0.1 localhost 127.0.1.1 exmaple-some.com 127.0.1.1 a.newdomain.com 127.0.1.1 b.newdomain.com # The following lines are desirable for IPv6 capable hosts ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters exmaple-some.com 所在行实际内容不定,所以无法匹配这行再往下插入。
sed 一般是对行进行处理,所以无法匹配\n# The following,查了资料说可以用跳转标记匹配多行,但我一脸懵逼没看懂用法。
