bash - 在 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_types
并valid_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
所以,我想找到一种方法来做到这一点,但时间更短。
希望您能够帮助我。提前致谢。
解决方案
谨慎起见,可能值得尝试将这些并行化以减少总时间。但是您可能会遇到其他瓶颈。
这未经测试,但希望可能足以帮助您试一试:
## 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"
推荐阅读
- google-cloud-datastore - Spring+GCP Datastore:我使用存储库,并希望在开发或测试期间将其配置为使用数据存储模拟器
- javascript - 是否可以检测到基类中调用 super() 的类?
- java - 如何将实体分布在 Spring-data-jpa 和 eclipselink 中的不同模块上?
- machine-learning - 如何在相机以外的其他传感器上使用 SLAM?
- mysql - mysql查询中的CASE函数
- ms-access - MSAccess - 清除多选列表框,移动到顶行,未选中
- jekyll - Jekyll - 从一个 Markdown 文件创建多个登陆页面
- c++ - 在使用 std::atomic::compare_exchange_strong 时,其他线程是否可以看到对 std::atomic 的写入?
- javascript - 在 HTML 中显示来自 API 的 JSON 数据
- dart - Flutter中“Widget build(BuildContext context)”的“context”是什么意思?