Skip to content

Commit

Permalink
Added checker script for external links (#53)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
HofiOne authored May 24, 2024
2 parents b17eb32 + 170dfac commit 7e41d07
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions _tools/linkcheck
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

check_link_file()
{
if ! [ -f "${EXTERNAL_LINK_FILE}" ]; then
echo "The specified file $EXTERNAL_LINK_FILE does not exist. Exiting..."
exit 1
fi
}


check_urls()
{
echo "Checking validity of external links..."

local URLS+=($(grep 'url:' "$EXTERNAL_LINK_FILE" | tail -n+1 | awk '{ print $2}'))

if [ ${#URLS[@]} == 0 ]; then
echo "No links found in $EXTERNAL_LINK_FILE, please check."
exit 1
fi

for url in "${URLS[@]}";
do
response=$(curl -o /dev/null -s -w "%{http_code}\n" $url)

if [[ "$response" == "000" || "$response" > "399" ]]; then
echo "($response) $url is not a live url, please check in $EXTERNAL_LINK_FILE."
INVALID=$((INVALID+1))
fi

sleep .5
done

if [[ "$INVALID" == 0 ]]; then
echo "Every link is alive and OK."
else
echo "$INVALID invalid links found."
exit 1
fi
}


if ! [ "$#" -eq 1 ]; then
echo "Usage: $0 <external link file>"
exit 1
fi

EXTERNAL_LINK_FILE="${1}"
INVALID=0

check_link_file
check_urls

exit 0

0 comments on commit 7e41d07

Please sign in to comment.