2017-03-01 101 views
-2

我應該編寫一個sh腳本,下載一個文件(大約100MB-100GB),並在mysql數據庫中每10秒寫入一次進度(百分比,下載速度,剩餘時間)。 有人能幫助我嗎?Shell下載腳本

謝謝!

有一個愉快的一天:)

+0

您好,歡迎堆棧溢出。我們不是代碼編寫服務。我們更喜歡,如果你告訴我們你嘗試了,我們會看到,如果我們可以幫助你。那麼,你有什麼嘗試?爲什麼它必須進入MySQL?似乎有點矯枉過正。 – Schwern

+0

對不起。我試過Wget,它下載我的文件,但現在我怎麼能得到狀態? Mysql不是必須的。登錄到文件也沒關係。 –

回答

1

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客戶端庫,它提供了一個進度回調一個小程序。以下是使用HTTP :: Tiny的Perl示例。

#!/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; 
    } 
}); 

大多數語言都有類似的東西。現在,您可以隨心所欲地記錄進度。

+0

非常感謝你的偉大答案!我想我會用第二種方法。現在我的最後一個問題是我可以從PHP啓動perl腳本嗎? –