简体   繁体   English

如何从URL捕获文件大小并在Shell脚本中使用它

[英]How to capture the file size from a URL and use it in Shell scripting

URL_C="any url"
length_C=$(curl -sI $URL_C | grep Content-Length | cut -d ' ' -f 2)
answer_C=$(awk -v len=$length_C 'BEGIN{printf "%.0f\n", len/1024}')

Will the variable $answer_C capture the file size ? 变量$answer_C捕获文件大小吗?

Is Content-Length the correct way ? Content-Length是正确的方法吗?

You can retrieve headers, then try to awk the Content-Length value as @user000001 suggested, and if not found, download file to count chars : 您可以提取标题,然后尝试awkContent-Length值@ user000001建议,如果没有找到,下载文件数字符:

url="your_url"
size=$(curl -sI "$url" | awk '/Content-Length/{gsub("\\r", ""); print $2}')
if [ -z "$size" ]; then 
    size=$(curl -s "$url" | wc -c)
fi
printf "%s : %d bytes\n" "$url" "$size"

Note : gsub("\\\\r", "") in the awk command is for removing carriage returns from the curl output. 注意:awk命令中的gsub("\\\\r", "")用于从curl输出中删除回车符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM