如何检查 shell 中是否存在 URL 并且可能卷曲? [英] How to check if an URL exists with the shell and probably curl?

查看:12
本文介绍了如何检查 shell 中是否存在 URL 并且可能卷曲?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个简单的 shell (+curl) 检查,如果 URL 存在(返回 200),它将评估为 true 或 false.

I am looking for a simple shell (+curl) check that would evaluate as true or false if an URL exists (returns 200) or not.

推荐答案

使用 --fail 将使失败请求的退出状态非零.使用 --head 将避免下载文件内容,因为我们不需要它来进行此检查.使用 --silent 将避免检查本身发出状态或错误.

Using --fail will make the exit status nonzero on a failed request. Using --head will avoid downloading the file contents, since we don't need it for this check. Using --silent will avoid status or errors from being emitted by the check itself.

if curl --output /dev/null --silent --head --fail "$url"; then
  echo "URL exists: $url"
else
  echo "URL does not exist: $url"
fi

<小时>

如果您的服务器拒绝 HEAD 请求,另一种方法是仅请求文件的第一个字节:


If your server refuses HEAD requests, an alternative is to request only the first byte of the file:

if curl --output /dev/null --silent --fail -r 0-0 "$url"; then

这篇关于如何检查 shell 中是否存在 URL 并且可能卷曲?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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