Chinaunix

标题: 利用cURL实现单个文件分多段同时下载,支持断点续传 [打印本页]

作者: snowtty    时间: 2007-04-01 08:19
标题: 利用cURL实现单个文件分多段同时下载,支持断点续传
下面是代码:(转自CU)
(俺是新手,高手见笑)
用法:./mycurl url
#!/bin/bash
######################################################################################################
#
# Script for curl to support resumable multi-part download.
#
url=$1
exename=`basename $0`
# How many "parts" will the target file be divided into?
declare -i parts=5
read -ep "Please input the target directory: " targetdir
read -ep "Please input the outfile name: " outfile
[ -z "$targetdir" ] && targetdir="./"
cd $targetdir||exit 2
[ -z "$outfile" ] && outfile=`basename $1`
length=`curl -s -I $url|grep Content-Length|sed s/[^0-9]//g`
#length=`curl -s -I $url|grep Content-Length|awk '{print $2}'|sed s/[^0-9]//`
if [ -z "$length" ]; then
        echo "cann't get the length of the target file"
        exit 1
fi
let "length = $length"
declare -i lsession=$(($length/$parts))
finished="false"
while true;
do
declare -i curr=0
for (( i=1; i> $outfile$i &
                fi
        else
                if [ $offset -lt $(($length-$(($lsession*$(($parts-1)))))) ]; then
                        curl -r $(($curr+$offset))- $url >> $outfile$i &
                fi
        fi
               
        curr=$(($curr+$lsession))                        
done
#To judge if all curl threads have terminated.
while true;
do
        hasCurl=`ps -A -o ppid,cmd|grep $$|grep -v "$exename"|grep -v "grep"|grep curl`
        [ -z "$hasCurl" ] && break
        sleep 10
done
finished="true"
for (( i=1; i> $outfile
        rm $outfile$i
done
echo "Done."


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/5591/showart_268964.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2