我管理着大量的网站,想要找到一种廉价的方式来让自己知道其中一个网站没有加载,从而最大限度地减少客户发现他们的网站在我之前宕机的次数。我决定创建一个for循环来加载域名,并输出返回200响应以外的任何值的域。有人能帮我学一下语法吗?我不知道该怎么做。
到目前为止,我相信它是:for i in $; do curl -L $;
除了上面所说的,我不确定如何获得对我每天检查域名的文件的响应。
您能提供的任何东西都将不胜感激。
发布于 2019-11-21 08:26:32
您可以定义此bash函数:
httpstatus() {
file=$1
# read file line by line
while IFS= read -r site; do
# print sites that do not return status 20X or 30X
if [[ $(curl -o /dev/null -sIw '%{http_code}\n' $site) != [23]0[0-2] ]]; then
echo $site
fi
done < "$file"
}示例用法:
$ cat sites
https://stackoverflow.com
https://example.com
https://example.com/returns-404
$ httpstatus sites
https://example.com/returns-404Bash语法和内置命令记录在man bash中。BashFAQ是其他有用的资源。
https://stackoverflow.com/questions/58960658
复制相似问题