【发布时间】:2016-06-04 04:37:43
【问题描述】:
所以我正在编写一个脚本,它将 grep 的输出作为一个数组,然后在其上迭代一个过滤器以输出到一个文件。我正在我自己的站点上对其进行测试,wget 按预期工作并在 spider.queue 中生成 URL 列表。 grep 命令也可以按关键字过滤,但是当我将它添加到 while 循环中并使用 if 语句检查它是否已经存在时,我会得到错误;
./spider.sh: 19: ./spider.sh: 语法错误:“(”意外(预期“完成”)
这会让我相信这是其中一个循环的语法问题。
#!/bin/sh
# Usage - ./spider.sh searchterm www.website.com
## Parameters
search=$1
URL=$2
## Spider WGET
wget -r -e robots=off --header="Accept: text/html" --user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:21.0) Gecko/20100101 Firefox/21.0" http://$URL 2>&1 | grep '^--' 2>&1 | awk '{ print $3 }' | grep -v '\.\(css\|js\|png\|gif\|jpg\|JPG\)$' >> spider.queue
## Keyword filter with grep
while true
do
PROFILES=($(grep -l -r "$search" $URL))
for x in ${PROFILES[*]}
do
if grep -q $x crawler.queue; then
echo "Already Exists"
else
$x >> crawler.queue
fi
done
done
【问题讨论】:
-
你的意思是
echo "$x" >> crawler.queue吗? -
你说得对,我应该把它放进去,错过了谢谢。不幸的是,我在第 19 行仍然遇到同样的错误
-
尝试引用所有变量,例如
wget ... "http://$URL"和grep -q "$x" crawler.queue -
上面的代码是不是简化了?您的
while true将永远运行。您用true替换的字符串可能有问题。
标签: bash while-loop grep do-while