用于搜索网页的 Bash 脚本

用于搜索网页的 Bash 脚本

我已经用 Google 搜索了我的问题,但情况变得越来越令人困惑。

我想编写一个 bash 脚本来搜索页面中查找特定字符串,如果找到则回显。例如,如果我想搜索页面上存在的字符串“Game Developer”,则它会回显“Found Game Developer!”

我甚至不知道从哪里开始。我正在使用 Lubuntu 14.04.2 LTS

我能想到的最好的办法是:

wget -q -O- http://www.centennialcollege.ca/programs-courses/centres-institutes/applied-research-and-innovation/for-students/job-postings/ | grep -c "Game Developer"

答案1

我改用 grep 的-q参数。如果发现某些内容,它只会以零退出。这允许您使用传统的退出代码逻辑(如&&或)||来链接操作。

wget -qO- http://www.centennialcollege.ca/programs-courses/centres-institutes/applied-research-and-innovation/for-students/job-postings/ \
| grep -q "Game Developer" \
&& echo "Found Game Developer"

只是读了一下评论。如果您想要计数-c(正如您所做的那样),那么效果就很好了,但是如果您只需要布尔输出,那么-q速度会更快,因为它会在第一次匹配时立即退出 0,而不是继续查找未来的匹配。

如果您想要捕获并输出计数,请将其分成两部分:

COUNT=$(wget -qO- http://www.centennialcollege.ca/programs-courses/centres-institutes/applied-research-and-innovation/for-students/job-postings/ | grep -c "Game Developer")
echo "Found $COUNT game developers"

相关内容