如何使用bash shell脚本检查文本文件中URL的状态 [英] How to check status of URLs from text file using bash shell script
问题描述
我必须检查200个http URL的状态,并找出其中哪些是断开的链接.链接显示在一个简单的文本文件中(例如,我的〜文件夹中存在URL.txt).我正在使用Ubuntu 14.04,并且是Linux新手.但是我知道bash shell非常强大,可以帮助我实现所需的功能.
I have to check the status of 200 http URLs and find out which of these are broken links. The links are present in a simple text file (say URL.txt present in my ~ folder). I am using Ubuntu 14.04 and I am a Linux newbie. But I understand the bash shell is very powerful and could help me achieve what I want.
我的确切要求是读取包含URL列表的文本文件,并自动检查链接是否正常运行,并将响应写入URL和其相应状态(工作/断开)的新文件中.
My exact requirement would be to read the text file which has the list of URLs and automatically check if the links are working and write the response to a new file with the URLs and their corresponding status (working/broken).
推荐答案
我创建了一个文件"checkurls.sh",并将其放置在我的urls.txt文件所在的主目录中.我使用
I created a file "checkurls.sh" and placed it in my home directory where the urls.txt file is also located. I gave execute privileges to the file using
$chmod +x checkurls.sh
checkurls.sh的内容如下:
The contents of checkurls.sh is given below:
#!/bin/bash
while read url
do
urlstatus=$(curl -o /dev/null --silent --head --write-out '%{http_code}' "$url" )
echo "$url $urlstatus" >> urlstatus.txt
done < $1
最后,我使用以下命令从命令行执行它-
Finally, I executed it from command line using the following -
$./checkurls.sh urls.txt
Voila!可以.
这篇关于如何使用bash shell脚本检查文本文件中URL的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!