删除由curl生成的文件中的行,该文件由my bash设置中的两个函数调用
我在Mac OS X上,使用called by删除由curl生成的文件中的行,该文件由my bash设置中的两个函数调用,bash,hosts,Bash,Hosts,我在Mac OS X上,使用called bycurl帮助生成我的主机文件。位于/etc/hosts.d/*的目录有几个文件: 1-warning // File of comments that explains how this whole thing works 2-hosts-original // The original OS X hosts file 3-adobe // Some Adobe hosts that flash talks to way too often 4-so
curl
帮助生成我的主机文件。位于/etc/hosts.d/*的目录有几个文件:
1-warning // File of comments that explains how this whole thing works
2-hosts-original // The original OS X hosts file
3-adobe // Some Adobe hosts that flash talks to way too often
4-someonewhocares // The results of the `curl` command in my below function
5-development // Any local dev work I am doing to make my own quick domains
当curl
运行时,会编写“4-someonewhocare”。我使用两个函数,因为主机curl
调用可能会变慢、变慢、中断等
这两个功能:
function write-hosts() {
cat /etc/hosts.d/* > /etc/hosts;
}
function update-hosts() {
curl http://someonewhocares.org/hosts/zero/hosts -o /etc/hosts.d/4-someonewhocares 2> /dev/null
write-hosts
}
每隔几天,我调用updatehosts
,它会写入一个新的“4-someonewhocare”文件,完成后,函数write hosts
cat
会将所有文件保存在/etc/hosts.d/*中,并将其输出到/etc/hosts
我想从“4-someonewhocares”文件中排除几行。例如:
www.googleadservices.com
feedads.googleadservices.com
我认为最好在write hosts
函数中删除行,因为在curl
完成之前,永远不会调用该函数。但是我也可以在curl
命令之后和write hosts
函数之前完成
我正在寻找关于从结果“4-someonewhocares”文件中删除行的最佳位置的建议,以及从文件中删除行的建议方法。理想情况下,我会在/etc/hosts.d中放置一个“6-whitelist”文件,但它会变得复杂,我不知道如何从cat
命令中排除它,尽管我可以按名称列出每个文件,因为它们不会改变
感谢您的建议。首先创建6-白名单:
$ cat /etc/hosts.d/6-whitelist
www.googleadservices.com
feedads.googleadservices.com
现在,修改write hosts
:
write-hosts() { grep -vhFf /etc/hosts.d/6-whitelist /etc/hosts.d/* >etc/hosts; }
工作原理
修订后的写入主机
从/etc/hosts.d/*
复制所有文件,但不包括在/etc/hosts.d/6-whitelist
中找到的任何行。使用的grep
选项有:
告诉grep从命名文件中获取要匹配的模式列表-f/etc/hosts.d/6-whitelist
告诉grep将这些模式视为固定字符串,而不是正则表达式。除非您需要正则表达式功能,否则这会加快速度-F
告诉grep不要打印行来自的文件名-h
反转匹配,以便只打印不在白名单中的行-v
function
。Bash不需要它,其他shell也不会接受它