贴一个批量下载脚本

来源:互联网 发布:诗洋seo 编辑:程序博客网 时间:2024/05/01 04:19

在网上下载时,可以发现有些有规律的连接,如以递增数字命名

http://xx...xx/chapter01.pdf

http://xx...xx/chapter02.pdf

....

http://xx...xx/chapter99.pdf

 

上面的连接模板为

http://xx...xx/chapter{}.pdf

把{}依次替换为01到99下载即可

 

下面这个脚本完成这个功能

 

#!/bin/bash

 

usage()

{

echo "usage:" >&2

echo "`basename $0` [-p positionwidth] urltemplate min max" >&2

exit 0

}

 

replace()

{

ins=$2

while [ $3 -gt ${#ins} ]; do

ins="0$ins"

done

echo $1 | sed -e "s/{}/$ins/"

}

 

poswid=0

while getopts ":p:" opt;

do

case $opt in

p)

poswid=$OPTARG

;;

?)

echo "invalid option -$OPTARG" >&2

usage

;;

esac

done

 

shift $((OPTIND-1));

if [ $# -ne 3 ]; then

usage

fi

 

urltpl="http://${1#http://}"

min=$2

max=$3

for i in `seq $min $max`

do

url=`replace $urltpl $i $poswid`

wget $url

done

按如下用法可批量下载上面提到的pdf文件
multiget -p 2 http://xx...xx/chapter{}.pdf 1 99
如果命名里面的递增数据没有对齐,则可以不需要-p参数,即
multiget http://xx...xx/chapter{}.pdf 1 99
可以下载
http://xx...xx/chapter1.pdf 
http://xx...xx/chapter2.pdf 
...
http://xx...xx/chapter99.pdf 

原创粉丝点击