首页 > 解决方案 > 在 bash 脚本中检查 URL 是否有效的最快方法

问题描述

我有一个名为test. 它看起来像这样(示例):

<http://es.dbpedia.org/resource/!!!> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/MusicalArtist> .
<http://es.dbpedia.org/resource/!!!_(álbum)> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Album> .
<http://es.dbpedia.org/resource/!Kung_Ekoka> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Language> .
<http://es.dbpedia.org/resource/!_(álbum)> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Album> .
<http://es.dbpedia.org/resource/!_(álbum_de_Trippie_Redd)> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Album> .
<http://es.dbpedia.org/resource/$9.99> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Film> .
<http://es.dbpedia.org/resource/$h*!_My_Dad_Says> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/TelevisionShow> .
<http://es.dbpedia.org/resource/%22A%22_de_adulterio> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Book> .
<http://es.dbpedia.org/resource/%22B%22_de_bestias> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Book> .
<http://es.dbpedia.org/resource/%22C%22_de_cadáver> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Book> .

我制作了一个脚本,该脚本在两个不同的文件中返回,invalid_instance_typesvalid_instance_types分别返回文件的有效和无效 URL 的三元组(每行只检查和test之间的第一个 URL )。<>

当 URL 无效时,如果单击它,将显示以下消息:No further information is available. (The requested entity is unknown)。示例:http ://es.dbpedia.org/resource/does_not_exist

这是我制作的脚本:

echo "Separating valid and invalid URLs"
while read line;
do
    url=`echo "$line" | cut -d' ' -f1 | cut -d'<' -f2 | cut -d'>' -f1`
    if ! wget -q --method=HEAD $url; then
        echo $line >> invalid_instance_types
    else
        echo $line >> valid_instance_types
    fi
done < test
echo "Done"

test问题是检查总共有 215 行的文件大约需要 1 分钟。

我一直在调查,需要更多时间执行的是 URL 检查行:

! wget -q --method=HEAD $url

所以,我想找到一种方法来做到这一点,但时间更短。

希望您能够帮助我。提前致谢。

标签: bashfile

解决方案


谨慎起见,可能值得尝试将这些并行化以减少总时间。但是您可能会遇到其他瓶颈。

这未经测试,但希望可能足以帮助您试一试:


## The fucntions does the checking. This will be called 
## as a async background process

function checkurl
{
    myline = $1
    myurl = $2
    if ! wget -q --method=HEAD $myurl; then
        echo $myline >> invalid_instance_types
    else
        echo $myline >> valid_instance_types
    fi
}

echo "Separating valid and invalid URLs"

$maxno=10
$cno = 0

while read line;
do
    url=`echo "$line" | cut -d' ' -f1 | cut -d'<' -f2 | cut -d'>' -f1`
    checkurl  "$line" "$url" &

    ## Optional - have  a limit for number of submissions
    ## Not foolproof but can be tuned to taste and developed
    ## to be more accurate and robust, e.g check number of jobs
    ## running and limiting that to maxno.

    ((cno=cno+1))
    if [ $cno -gt $maxno ]
    then
       sleep 5
       cno=0
    fi

done < test

## Wait for all submitted processes to complete - using the 'while'
## loop might play tricks with this.
## If so, you could implement a sleep loop to periodically count
## the lines until
##   (wc -l test == (wc -l invalid_instance_types valid_instance_types))

echo "Waiting for all to complete"
wait
echo "Done"

推荐阅读