linux samba 断点续传,利用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<=parts; i=i+1 ))

do

#To get how many bytes have been downloaded

if [ -e $outfile$i ]; then

offset=`ls -l $outfile$i|awk '{print $5}'`

else offset=0

fi

let "offset = $offset"

if [ $i -lt $parts ]; then

if [ $offset -lt $lsession ]; then

curl -r $(($curr+$offset))-$(($curr+$lsession-1)) $url >> $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<=$parts; i=i+1 ))

do

if [ -e $outfile$i ]; then

offset=`ls -l $outfile$i|awk '{print $5}'`

else offset=0

fi

let "offset = $offset"

if [ $i -lt $parts ]; then

if [ $offset -lt $lsession ]; then

finished="false"

break

fi

else

if [ $offset -lt $(($length-$(($lsession*$(($parts-1)))))) ]; then

finished="false"

break

fi

fi

done

if [ "$finished" == "true" ]; then

break

fi

done

echo "All parts have been downloaded. Merging..."

mv --backup=t $outfile"1" $outfile

for (( i=2; i<=$parts; i=i+1))

do

cat $outfile$i >> $outfile

rm $outfile$i

done

echo "Done."

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值