LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Dead link checker in bash (https://www.linuxquestions.org/questions/programming-9/dead-link-checker-in-bash-919073/)

sopier 12-16-2011 10:11 AM

Dead link checker in bash
 
Hi, I have a code like this:

Code:

#!/bin/bash

function isLive {
    wget -q --spider $1
}

isLive "http://supremecourtofindia.nic.in/ecommittee/25%20Salient%20Features%20of%20Ubuntu%20Linux%20OS%20DVD.pdf"

if [ $? -eq 0 ]; then
    echo "Good link"
else
    echo "Broken link"
fi

The link is live, I can download the pdf using browser, but the script can't process the url, and hang...

Any solution on this?

H_TeXMeX_H 12-16-2011 10:16 AM

Try:

Code:

wget -q --spider --user-agent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1" http://supremecourtofindia.nic.in/ecommittee/25%20Salient%20Features%20of%20Ubuntu%20Linux%20OS%20DVD.pdf

sopier 12-16-2011 10:36 AM

Thank you... it works like charm.... :)

sopier 12-16-2011 07:38 PM

Hey, I'm improving the script like below:

Code:

#!/bin/bash

function isLive {
    wget -q --spider --spider --connect-timeout=5 --user-agent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1" $1
}

isLive "http://reksio.ftj.agh.edu.pl/~azrael/ubuntu/marketing/po_polsku/wersje%20robocze/Ubuntu_wallet.pdf"

if [ $? -eq 0 ]; then
    echo "Good link"
else
    echo "Broken link"
fi

and when I run the script, it freeze out... How to set timeout properly? So, if after xx seconds frozen, it can be categorized as broken links...

sopier 12-16-2011 07:44 PM

Never mind, it's solved, I add the --tries=xx argument in the wget option, so the script will looks like this:

Code:

#!/bin/bash

function isLive {
    wget -q --spider --spider --connect-timeout=5 --tries=3 --user-agent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1" $1
}

isLive "http://reksio.ftj.agh.edu.pl/~azrael/ubuntu/marketing/po_polsku/wersje%20robocze/Ubuntu_wallet.pdf"

if [ $? -eq 0 ]; then
    echo "Good link"
else
    echo "Broken link"
fi


grail 12-17-2011 10:14 PM

As you are using bash you can also use 'if' to do the test for you:
Code:

if command; then
    <if true>
else
    <if false>
fi



All times are GMT -5. The time now is 05:30 AM.