Skip to content

Commit 7e41d07

Browse files
authored
Added checker script for external links (#53)
Fixes #51 Usage: linkcheck \<external link file\> Parses the links from the external link file and gets the http statuses for each site via curl. If a site is unreachable or returns with an http error, a warning is printed.
2 parents b17eb32 + 170dfac commit 7e41d07

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

_tools/linkcheck

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/bash
2+
3+
check_link_file()
4+
{
5+
if ! [ -f "${EXTERNAL_LINK_FILE}" ]; then
6+
echo "The specified file $EXTERNAL_LINK_FILE does not exist. Exiting..."
7+
exit 1
8+
fi
9+
}
10+
11+
12+
check_urls()
13+
{
14+
echo "Checking validity of external links..."
15+
16+
local URLS+=($(grep 'url:' "$EXTERNAL_LINK_FILE" | tail -n+1 | awk '{ print $2}'))
17+
18+
if [ ${#URLS[@]} == 0 ]; then
19+
echo "No links found in $EXTERNAL_LINK_FILE, please check."
20+
exit 1
21+
fi
22+
23+
for url in "${URLS[@]}";
24+
do
25+
response=$(curl -o /dev/null -s -w "%{http_code}\n" $url)
26+
27+
if [[ "$response" == "000" || "$response" > "399" ]]; then
28+
echo "($response) $url is not a live url, please check in $EXTERNAL_LINK_FILE."
29+
INVALID=$((INVALID+1))
30+
fi
31+
32+
sleep .5
33+
done
34+
35+
if [[ "$INVALID" == 0 ]]; then
36+
echo "Every link is alive and OK."
37+
else
38+
echo "$INVALID invalid links found."
39+
exit 1
40+
fi
41+
}
42+
43+
44+
if ! [ "$#" -eq 1 ]; then
45+
echo "Usage: $0 <external link file>"
46+
exit 1
47+
fi
48+
49+
EXTERNAL_LINK_FILE="${1}"
50+
INVALID=0
51+
52+
check_link_file
53+
check_urls
54+
55+
exit 0

0 commit comments

Comments
 (0)