Shell下载脚本

时间:2017-03-01 20:53:34

标签: mysql shell file download wget

我应该编写一个下载文件(大约100MB - 100GB)的sh脚本,并在mysql数据库中每10秒写入一次进度(百分比,下载速度,剩余时间)。 有人可以帮助我吗?

谢谢!

度过愉快的一天:)

1 个答案:

答案 0 :(得分:2)

wget--progress选项,但它适用于正在观看进度的人。 curl也有一个进度表,但同样也适用于人类。

您可以使用wget -o将进度信息发送到日志文件,并解析该文件。

--2017-03-01 13:13:21--  http://download.thinkbroadband.com/1GB.zip
Resolving download.thinkbroadband.com (download.thinkbroadband.com)... 80.249.99.148
Connecting to download.thinkbroadband.com (download.thinkbroadband.com)|80.249.99.148|:80... connect
HTTP request sent, awaiting response... 200 OK
Length: 1073741824 (1.0G) [application/zip]
Saving to: ‘1GB.zip’

     0K .......... .......... .......... .......... ..........  0%  150K 1h56m
    50K .......... .......... .......... .......... ..........  0%  308K 86m34s
   100K .......... .......... .......... .......... ..........  0% 2.31M 60m10s
   150K .......... .......... .......... .......... ..........  0%  348K 57m41s
   200K .......... .......... .......... .......... ..........  0% 3.34M 47m10s

你必须编写一个程序来解析它。

使用提供进度回调的HTTP客户端库编写一个小程序更简单,更灵活。这是Perl中使用HTTP :: Tiny的一个例子。

#!/usr/bin/perl

use strict;
use warnings;
use v5.10;
use HTTP::Tiny;

my $url = "http://download.thinkbroadband.com/100MB.zip";

# A tiny HTTP client.
my $http = HTTP::Tiny->new;

# Track how many bytes have been received.
my $completed = 0;
$http->request("GET", $url, {
    # Add a callback when the next data hunk is received.
    data_callback => sub {
        # The data hunk, and info about the request.
        my($chunk, $response) = @_;

        # Add to the total received.
        $completed += length $chunk;

        # Get the total length (this won't always be available)
        my $length = $response->{'headers'}{'content-length'} || '?';

        # Calculate the percent received.
        my $percent = $completed / $length * 100;

        # Print it, making sure not to print too many decimal places.
        printf "%d of %d - %.2f%%\n", $completed, $length, $percent;

        return;
    }
});

大多数语言都有类似的东西。现在,您可以随心所欲地记录进度。