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.
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).
Herewith my full script that checks URLs listed in a file passed as an argument e.g. 'checkurls.sh listofurls.txt'.
What it does:
Code:
Please note that changing order of listed urls in text file will affect any existing lock files (remove all .lock files to avoid confusion). It would be improved by using url as file name but certain characters such as : @ / ? & would have to be handled for operating system.
if your input file contains one url per line you can use a script to read each line, then try to ping the url, if ping success then the url is valid
ping options :
you can use this option as well to limit waiting time
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
The contents of checkurls.sh is given below:
Finally, I executed it from command line using the following -
$./checkurls.sh urls.txt
Voila! It works.
What about to add some parallelism to the accepted solution. Lets modify the script
chkurl.sh
to be little easier to read and to handle just one request at a time:And now you check your list using:
This could finish the job up to 4 times faster.
Usage:
Sample:
Output:
For everything, read the Bash Manual. See
man curl
,help
,man bash
as well.