Skip to content

Instantly share code, notes, and snippets.

@eungjun-yi
Last active November 26, 2015 01:08
Show Gist options
  • Save eungjun-yi/2cbf748a6e68f3a958d5 to your computer and use it in GitHub Desktop.
Save eungjun-yi/2cbf748a6e68f3a958d5 to your computer and use it in GitHub Desktop.
Detecting broken links
#!/bin/sh
while getopts v OPTION ;
do
case "$OPTION" in
v) VERBOSE=true ; shift ;;
esac
done
urls=$(grep -i -E -o 'https?://[^ )]*[^), ]' $1)
# HEAD may be better for performance but sometimes does not work.
method="GET"
for url in $urls
do
test $VERBOSE && echo test: $url
status_line=`curl -s -I -X $method $url | head -n1`
result1=$?
test $VERBOSE && echo status: $status_line
echo $status_line | grep '^\S* [23][0-9][0-9]' > /dev/null
result2=$?
[ $result1 -ne 0 -o $result2 -ne 0 ] && echo $url
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment