【问题标题】:While Loop in Bash Unexpected Character在 Bash 意外字符中循环
【发布时间】: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


【解决方案1】:

/bin/sh不支持数组,所以语法错误是由PROFILES=(...引起的。

切换到#!/bin/bash

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 2015-08-06
    • 2021-03-02
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 2012-10-04
    相关资源
    最近更新 更多