-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurlcheck.sh
executable file
·172 lines (147 loc) · 4.49 KB
/
urlcheck.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/bash
#
# urlcheck.sh -- Check files for invalid URLs.
#
# Copyright (c) 2018 Irina Gulina
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#Functions
display_help() {
cat <<-END
This script looks for invalid URLs.
Usage:
------
-h
Display this help
-p
A path to look for URLs.
-I
A path to exclude from searching URLs.
A string separated by commas without spaces.
-i
A path to a file with ignored URLs.
Default: ./ignore_url.yml. If a file doesn't exist,
all found URLs are checked.
-a
Ignore a file with ignored URLs, i.e. check all URLs.
-t
File types to check for URLs.
A string separated by commas without spaces.
Default: *.md,*rst
-q
Quite. Default: false.
-v
Verbose. Default: false.
END
}
#Default Values
# Path to exclude from searching URLs.
# A string separated by commas without spaces.
exclude_path="vendor,./vendor"
# File types to check for URLs.
# A string separated by commas without spaces.
file_types="*.md,*.rst"
# A path to a file with ignored URLs.
ignore_urls="./ignore_url.yml"
# A path to look for URLs.
check_path="."
# Regex to find URL
url_regex="https?://[a-zA-Z0-9./?=_-]*"
#url_regex="(^|[^\`])\bhttps?://[a-zA-Z0-9./?=_-]*"
# Not be verbose by default
verbose=false
# Not be quite by default
quite=false
# Default logfile name
logfile=urlcheck.log
# Parse arguments
while getopts ":p:i:t:I:haq:" opt; do
case $opt in
h)
display_help && exit 0
;;
a)
ignore_urls=""
;;
q)
quite=true
;;
p)
check_path="${OPTARG}"
;;
i)
ignore_urls="${OPTARG}"
;;
I)
exclude_path="${OPTARG}"
;;
t)
file_types="${OPTARG}"
;;
\?)
echo "Invalid option: -${OPTARG}"
;;
:)
echo "Option -${OPTARG} requires an argument. Use -h for more details."
esac
done
#Find URLs
input=$(eval grep -EHori --exclude-dir={$exclude_path} \
--include={$file_types} \'$url_regex\' "$check_path" | sed 's/:[^a-z]/:/g')
#Build a dictionary of unique URLs
declare -A urlmap
i=1
sp="/-\|"
echo -n ' '
for entry in $(echo -e "$input"); do
$quite && printf "\rCalculating an array of URLs \b${sp:i++%${#sp}:1}"
page=$(echo $entry | cut -d ':' -f 1)
url=$(echo $entry | cut -d ':' -f 2- | sed -e 's/[.]*$//')
if [ -z "${urlmap[$url]}" ]; then
urlmap[$url]=$page
else
urlmap[$url]=${urlmap[$url]},\ $page
fi
done
#Print a number of unique URLs to check
echo -e "\rA number of unique URLs to check: ${#urlmap[@]}"
progress_index=0
invalid_url_index=0
for key in "${!urlmap[@]}"; do
#Check curl code of URL header
if ! curl -sIf -m 5 --retry 1 -o /dev/null "$key" || \
! curl -sIfL -m 5 --retry 1 -o /dev/null "$key"; then
#Record not ignored URL
if [ -z "$ignore_urls" -o ! -f "$ignore_urls" ] || \
! grep -Fxq "$key" "$ignore_urls" ; then
echo "Error: $key in ${urlmap[$key]}"
invalid_url_index=$((invalid_url_index+1))
fi
fi
#Print a percent of checked URLs
$quite && echo -ne "$(expr $progress_index "*" 100 "/" ${#urlmap[@]})"'%\r'
progress_index=$((progress_index+1))
done
#Print a number of found invalid URLs
if [ "$invalid_url_index" != 0 ]; then
echo "A number of invalid URLs: $invalid_url_index"
else
echo "No invalid URL found."
fi