有时候我们需要下载一个或者多个网站的资源到本地,需要保存对应的路径。方便一键替换,例如一些资源网的图片,和图床的图片等等。
https://rpg.pic-imges.com/pic/upload/vod/2020-04/1586778123.jpg
https://tva3.sinaimg.cn/large/87c01ec7gy1frqe5h4nvhj21hc0u0npd.jpg
这个时候我们可以用一个shell脚本解决问题。
将需要下载的文件地址输入进去,一行一个。
https://rpg.pic-imges.com/pic/upload/vod/2020-04/1586778123.jpg https://tva3.sinaimg.cn/large/87c01ec7gy1frqe5h4nvhj21hc0u0npd.jpg http://su.bdimg.com/static/superplus/img/logo_white_ee663701.png http://su.bdimg.com/static/superplus/img/logo_white_ee663704.png http://su.bdimg.com/static/superplus/img/logo_white_ee663705.png http://su.bdimg.com/static/superplus/img/logo_white_ee663706.png
我们需要下载这些图片,并保存在各自的文件夹下。
将下列代码输入进去。
#!/bin/bash
# desc: download resource
# author: 十年后的卢哥哥
mydir=`pwd`
while read line
do
{
if [ -n "$line" ]
then
cd $mydir
url=$(echo "$line" | tr -d '\r')
picdir=$(echo $url | sed -r 's/http:\/\///g')
picname=$(echo ${picdir##*/})
picpath=$(echo ${picdir%/*})
mkdir -p $picpath
cd $picpath
wget -O $picname `echo $url`
fi
}
done < $1
exit 0
这里有几点要注意:
必须在Linux环境下创建,不可以在Windows下创建脚本,否则会不兼容,脚本报错。
1、为了去掉文本文件中行末的换行符,要进行删除:
tr -d '\r'
2、取资源名:
${picdir##*/}3、取资源路径:
${picdir%/*}sh download.sh url.txt
便可看到sh脚本所在目录下会出现对应文件夹和内容。