Shell脚本实现批量下载资源并保留原始路径
技术  /  管理员 发布于 6年前   214
示例资源列表
如url.txt:
http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png
http://su.bdimg.com/static/superplus/img/logo_white_ee663703.png
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
我们需要下载这些图片,并保存在各自的文件夹下。
脚本如下
如download.sh
代码如下:
#!/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
这里有几点要注意:
1、为了去掉文本文件中行末的换行符,要进行删除:
代码如下:
tr -d '\r'
2、取资源名:
代码如下:
${picdir##*/}
3、取资源路径:
代码如下:
${picdir%/*}
运行
代码如下:
sh download.sh url.txt
原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..博主 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 @ mashrdn 多切换几个节点测试,免费ssr是没那么稳..Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号