Last active
November 12, 2020 00:47
-
-
Save siberex/318db26ffc6d2665607c46190d0a231c to your computer and use it in GitHub Desktop.
Check list of URLs
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
#!/usr/bin/env bash | |
set -euo pipefail | |
CMD_NAME=${0##*/} | |
usage() { | |
cat <<USAGE >&2 | |
Check each URL from list of URLs. | |
Prints out if HEAD request were successful or not. | |
Usage: | |
$CMD_NAME [--filter] /path/to/url_list.txt | |
--filter Optional. Filters out URLs which are returning 200 and prints them out. | |
Otherwise will print out each URL along with HTTP code in fancy colors. | |
--help Prints this help | |
URL list could be provided through STDIN: | |
cat /path/to/url_list.txt | $CMD_NAME [--filter] | |
USAGE | |
exit 1 | |
} | |
# Fancy colors | |
blu="\e[94m" # Light Blue | |
red="\e[1;91m" # Bold Red | |
clr="\e[0m" # Reset | |
function printok() { | |
printf "✅ ${blu}%s${clr}\n" "$*" | |
} | |
function printerr() { | |
printf "❌ ${red}%s${clr}\n" "$*" >&2 | |
} | |
FILTER=0 | |
URLS= | |
# Process arguments | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
--filter) | |
FILTER=1 | |
shift 1 | |
;; | |
--help) | |
usage | |
;; | |
*) | |
URLS="$1" | |
shift 1 | |
;; | |
esac | |
done | |
while read -r URL; do | |
RES=$(curl --head --silent --location --output /dev/null --write-out "%{http_code}" "${URL}" 2>/dev/null || true) | |
if [ "$RES" = "200" ]; then | |
[[ $FILTER -eq 0 ]] && printok "$RES" "$URL" || echo "${URL}" | |
else | |
[[ $FILTER -eq 0 ]] && printerr "$RES" "$URL" | |
fi | |
done < <(grep -i "^https\?://" "${URLS:-/dev/stdin}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment