如何使用 shell 脚本确定网页是否存在? [英] How do I determine if a web page exists with shell scripting?

查看:50
本文介绍了如何使用 shell 脚本确定网页是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个程序,可以将一系列漫画扫描文件转换为一个 pdf 文件,我不想尝试下载图片来确定我是否有正确的 url.是否有可用于检查网页是否存在的 shell 脚本命令?

I am trying to make a program that can convert a series of manga scans into one pdf file, and I don't want to have to attempt to download the picture to determine if I have the right url. Is there a shell scripting command that I can use to just check if a web page exists?

推荐答案

在 *NIX 下,您可以使用 curl 发出一个简单的 HEAD 请求(HEAD 只要求标题,而不是页面正文):

Under a *NIX, you can use curl to issue a simple HEAD request (HEAD only asks for the headers, not the page body):

curl --head http://myurl/

那么你可以只取第一行,其中包含 HTTP 状态代码(200 OK、404 Not Found 等):

Then you can take only the first line, which contains the HTTP status code (200 OK, 404 Not Found, etc.):

curl -s --head http://myurl/ | head -n 1

然后检查您是否得到了不错的回复(状态代码是 200 或 3**):

And then check if you got a decent response (status code is 200 or 3**):

curl -s --head http://myurl/ | head -n 1 | grep "HTTP/1.[01] [23].."

如果状态代码正常,这将输出第一行,否则将不输出任何内容.您也可以将其通过管道传输到/dev/null 以获取不输出,并使用 $? 来确定它是否有效:

This will output the first line if the status code is okay, or nothing if it isn't. You can also pipe that to /dev/null to get no output, and use $? to determine if it worked or no:

curl -s --head http://myurl/ | head -n 1 | grep "HTTP/1.[01] [23].." > /dev/null
# on success (page exists), $? will be 0; on failure (page does not exist or
# is unreachable), $? will be 1

EDIT -s 只是告诉 curl 不要显示进度条".

EDIT -s simply tells curl to not show a "progress bar".

这篇关于如何使用 shell 脚本确定网页是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆