在 Bash 中循环出现意外字符 [英] While Loop in Bash Unexpected Character

查看:33
本文介绍了在 Bash 中循环出现意外字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在编写一个脚本,它将把 grep 的输出作为一个数组,然后在它上面迭代一个过滤器以输出到一个文件.我正在我自己的网站上测试它,wget 按预期工作,并在 spider.queue 中生成一个 URL 列表.grep 命令也可以按关键字过滤,但是当我将它添加到 while 循环中并使用 if 语句检查它是否已经存在时,我会得到错误;

So I'm writing a script that will take the output of a grep as an array and then iterate a filter over it to output to a file. I'm testing it on my own site and the wget works as expected and generates a list of URLS in the spider.queue. The grep command will also work in terms of filtering by a keyword, but when I add it into a while loop and use an if statement to check if it already exists I'll get the error;

./spider.sh: 19: ./spider.sh: 语法错误:("意外(期望完成")

./spider.sh: 19: ./spider.sh: Syntax error: "(" unexpected (expecting "done")

这会让我相信这是其中一个循环的语法问题.

Which would lead me to believe it's a syntax problem with one of the loops.

#!/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

推荐答案

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

/bin/sh does not support arrays, so the syntax error is caused by PROFILES=(....

切换到#!/bin/bash.

这篇关于在 Bash 中循环出现意外字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆