bash 如何通过linux shell检查url是否有效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21597851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:29:01  来源:igfitidea点击:

How to check whether url is working or not, through linux shell

linuxbash

提问by user3209101

How to check whether a particular url is woking or not, through linux shell? I tried through elinks, but it shows:

如何通过 linux shell 检查特定的 url 是否有效?我尝试通过 elinks,但它显示:

ERROR: This application requires javascript enabled in the browser

错误:此应用程序需要在浏览器中启用 javascript

回答by MLeFevre

You can do it easily using curl (you may have to install curl if you system doesn't already have it).

您可以使用 curl 轻松完成(如果您的系统还没有安装 curl,您可能需要安装它)。

for example, performing

例如,执行

curl -Is [URL YOU WANT TO CHECK] | head -n 1

will return you a http status code. You can then determine the url exists or not from this status code. For example:

将返回一个http 状态代码。然后,您可以根据此状态代码确定 url 是否存在。例如:

curl -Is http://stackoverflow.com/questions/21597851/how-to-check-whether-url-is-working-or-not-through-linux-shell | head -n 1

i.e, your question, will return a 200 status code. HTTP/1.1 200 OK

即,您的问题将返回 200 状态代码。 HTTP/1.1 200 OK

But another url such as

但是另一个网址,例如

curl -Is http://stackoverflow.com/questions/madeupquestion/example | head -n 1

will return a 404, as it doesn't exist. HTTP/1.1 404 Not Found

将返回 404,因为它不存在。 HTTP/1.1 404 Not Found