From a1fc1ba9508ffe3f495459b0750f1637b28fab57 Mon Sep 17 00:00:00 2001 From: Marc Wrobel Date: Mon, 1 Aug 2022 19:20:22 +0200 Subject: [PATCH] Initial version of the script Code copied from https://github.com/marcwrobel/jbanking/blob/2030a902dcd8c396a29520482cf2cbe94d2428a6/bin/check-links. --- README.md | 7 ++++++- checklinks | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100755 checklinks diff --git a/README.md b/README.md index ce385c5..409fa48 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ # checklinks -Helps check and fix links in a codebase. + +A project codebase often contains a lot of external links. checklinks is an utility that helps checking and fixing those links. + +```shell +path/to/checklinks +``` diff --git a/checklinks b/checklinks new file mode 100755 index 0000000..2f37c5a --- /dev/null +++ b/checklinks @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +USER_AGENT='Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0' +RETRY=1 +RETRY_DELAY=10 +TIMEOUT=3 # seconds + +RED='\033[0;31m' +GREEN='\033[0;32m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +[ ! -d "$1" ] && echo "'$1' is not a directory" && exit 1 + +# Links are processed in a random order to reduce the risk of being blacklisted and temporarily blocked +for url in $(grep -RioEh 'https?://[^][{} "`<>),*$|\\]*[^][{} "`<>),*$|\\.:'"'"']' | grep -vE 'https?://(localhost|[0-9]+|.+:[0-9]+|example|host|somehost|nohost|link|acme.org|foo.com|application.com|[^/]+.example.com|registry.npmjs.org|apache.org/xml/features|java.sun.com/xml/ns|javax.xml.XMLConstants)' | sort | uniq | sort -R); do + # we could use --head, but it is not always supported... + status=$(curl -o /dev/null --silent --connect-timeout "$TIMEOUT" --retry $RETRY --retry-delay $RETRY_DELAY --user-agent "$USER_AGENT" --location --write-out '%{http_code}' "$url") + + if [ "$status" = "200" ]; then + if [[ $url =~ "http://" ]]; then + echo -e "${BLUE}$url ($status)${NC}" + else + echo -e "${GREEN}$url ($status)${NC}" + fi + else + echo -e "${RED}$url ($status)${NC}" + fi +done