Last active
December 30, 2016 16:46
-
-
Save chzyer/7018153 to your computer and use it in GitHub Desktop.
检查本地死链接
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
host="http://docs.qiniu.com" | |
IFS=$(echo -en "\n\b") | |
code=0 | |
for file in `find $1 -name *.html`; do | |
for href in `egrep -o 'href="(.*?)".*?>(.*?)<' < $file | sed -E 's/href="([^\"]*)".*>(.*)</\1•\2/g'`; do | |
name=${href##*•} | |
href=${href%%•*} | |
if [ ${href:0:1} == "#" ]; then | |
path=$file$href | |
elif [ ${href:0:21} == "$host" ]; then | |
path=$1${href:21} | |
elif [ ${href:0:4} == "http" ]; then | |
#ignore url | |
continue | |
elif [ ${href:0:6} == "mailto" ]; then | |
continue | |
elif [ ${href:0:1} == "/" ]; then | |
path=$1$href | |
else | |
path="$(dirname $file)/$href" | |
fi | |
anthor=${path##*#} | |
if [ "$anthor" == $path ]; then anthor=""; fi | |
path=${path%%#*} | |
if [ ${path: -1} == "/" ]; then path="${path}index.html"; fi | |
if [ ! -f $path ]; then | |
code=1 | |
echo "missing file: $path, file: $file, link: $href, name: $name" | |
continue | |
fi | |
if [ ! -z $anthor ]; then | |
s=`cat $path |egrep -o "(name|id)=\"$anthor\""` | |
if [ -z $s ]; then | |
code=1 | |
echo "missing anthor: $anthor, file: $file, link: $href, name: $name" | |
fi | |
fi | |
done | |
done | |
exit $code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment