简单的 bash 和 curl 检查文件是否存在于 Web 服务器上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33954612/
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 13:57:30  来源:igfitidea点击:

Simple bash and curl check that a file exists on a web server?

bashhttpcurl

提问by sorin

I am trying to improve a WebDAV bash uploader to bypass uploading files when they already exist on the web server.

我正在尝试改进 WebDAV bash 上传器,以在 Web 服务器上已经存在文件时绕过上传文件。

What should work with a HEAD request and parsing this after this.

什么应该与 HEAD 请求一起使用并在此之后解析它。

Extra bonus if the solution would also include size verification.

如果解决方案还包括尺寸验证,则额外奖励。

回答by rkrzr

Quick and dirty: Just check if a Content-Length header is in the response?

快速而肮脏:只需检查响应中是否包含 Content-Length 标头?

Something like:

就像是:

curl -I http://google.com | grep Content-Length

curl -I http://google.com | grep Content-Length

Then potentially parse the length using awk:

然后可能使用 awk 解析长度:

curl -I http://google.com | grep Content-Length | awk -F ': ' '{print $2}'

curl -I http://google.com | grep Content-Length | awk -F ': ' '{print $2}'

This give you the length of the content in bytes (if the file exists) or otherwise an empty string.

这为您提供以字节为单位的内容长度(如果文件存在)或其他空字符串。

回答by Matt

status=$(curl --head --silent http://domain/object | head -n 1)
if echo "$status" | grep -q 404
  echo "Uploading"
else
  echo "Not uploading: $status"
fi

回答by sorin

Here is some bash code that checks if the files exists on the webserver and if it does have the same size as the one one disk.

这是一些 bash 代码,用于检查文件是否存在于网络服务器上,以及它是否与一个磁盘的大小相同。

The file is not downloaded, only checked using a HEAD request.

该文件不会被下载,只会使用 HEAD 请求进行检查。

TMPFILE=$(tempfile)
FILE=location/some_file_to_upload.txt
MYVAR=http://example.com/$FILE

response=$(curl -n --silent -X HEAD $MYVAR --write-out "\n%{http_code}\n" -D $TMPFILE)
status_code=$(echo "$response" | sed -n '$p')

LOCAL_SIZE=$(stat -c%s "$FILE")
SERVER_SIZE=$(cat $TMPFILE|grep -i "Content-Length" | awk 'BEGIN {RS=""}{gsub(/\r/,"",##代码##); print ##代码##}' | awk -F ': ' '{print }')
if [ -z "$VAR" ]; then
       SERVER_SIZE=0
fi

if [ "$status_code" -lt "200" -o "$status_code" -gt "299" -o "$LOCAL_SIZE" -ne "$SERVER_SIZE" ]; then
        echo 'file does not exist or is of different size
fi

回答by sorin

You can also just output the reported size, note that in the case of http://google.comyou will get a low byte count of 219, that's because it's a 301 redirect. To get curl to follow the links to the actual file and report the file size you can use the -L --post301options:

您也可以只输出报告的大小,请注意,如果http://google.com您将获得 219 的低字节数,那是因为它是 301 重定向。要让 curl 跟随指向实际文件的链接并报告文件大小,您可以使用以下-L --post301选项:

curl -s -o /dev/random -w '%{size_download}' http://google.com

curl -s -o /dev/random -w '%{size_download}' http://google.com

219

219

curl -L --post301 -s -o /dev/random -w '%{size_download}' http://google.com

curl -L --post301 -s -o /dev/random -w '%{size_download}' http://google.com

18942

18942

(Note: On non-Linux systems change /dev/randomto /dev/null.)

(注意:在非 Linux 系统上更改/dev/random/dev/null.)