Skip to content

Instantly share code, notes, and snippets.

@akccakcctw
Last active September 26, 2017 08:30
Show Gist options
  • Save akccakcctw/62d0371a6585ddda537f273e89248a2a to your computer and use it in GitHub Desktop.
Save akccakcctw/62d0371a6585ddda537f273e89248a2a to your computer and use it in GitHub Desktop.
check URLs in a file and get HTTP status code
#!/bin/bash
# -----------------------
# |###| version 1.3.1
# |###| Rex Tsou
# |###| 2017/09/26
# usage------------------
# $ ./checkurls.sh [file]
# -----------------------
output_file='url_status.md'
# colorful echo
colorDefault='\033[0m'
colorRevert='\033[7m'
grep -Eo '(http|https)://[^" ]+' < $1 >> "~tmp${output_file}"
urlLen=$(wc -l < "~tmp${output_file}") # get count of URLs
urlCurrent=0
echo "There are ${urlLen} URLs in $1"
echo "Starting to check.."
echo '' >> $output_file
echo `date` >> $output_file
echo $1 >> $output_file
echo '----' >> $output_file
while IFS= read url || [[ -n "$url" ]];
do
echo -ne "${revertColor}[ $((++urlCurrent))/${urlLen} ]${noColor}"\\r # show progress
url=${url%$'\r'} # trim CR(\r) at the end
urlstatus=$(curl --output /dev/null --silent --head --write-out '%{http_code}\n' "$url")
if [[ $urlstatus =~ ^000$ ]] ; then # Regular Expression check http status code
echo "$url __"$urlstatus"__ No valid HTTP response <<<<<" >> $output_file
elif [[ $urlstatus =~ ^3..$ ]] ; then
echo "$url __"$urlstatus"__ Redirection" >> $output_file
elif [[ $urlstatus =~ ^4..$ ]] ; then
echo "$url __"$urlstatus"__ Client Error <<<<<" >> $output_file
elif [[ $urlstatus =~ ^5..$ ]] ; then
echo "$url __"$urlstatus"__ Server Error <<<<<" >> $output_file
else
echo "$url __"$urlstatus"__" >> $output_file
fi
done < "~tmp${output_file}"
echo '----' >> $output_file
rm "~tmp${output_file}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment