Curl has a specific option, --write-out
, for this:
$ curl -o /dev/null --silent --head --write-out '%{http_code}
' <url>
200
-o /dev/null
throws away the usual output
--silent
throws away the progress meter
--head
makes a HEAD HTTP request, instead of GET
--write-out '%{http_code}
'
prints the required status code
To wrap this up in a complete Bash script:
#!/bin/bash
while read LINE; do
curl -o /dev/null --silent --head --write-out "%{http_code} $LINE
" "$LINE"
done < url-list.txt
(Eagle-eyed readers will notice that this uses one curl process per URL, which imposes fork and TCP connection penalties. It would be faster if multiple URLs were combined in a single curl, but there isn't space to write out the monsterous repetition of options that curl requires to do this.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…