欢迎来到站长天空!

其它服务器

当前位置: 主页 > 服务器 > 其它服务器

Shell脚本实现批量下载资源并保留原始路径

时间:2025-12-31 17:30:04|栏目:其它服务器|点击:

示例资源列表
如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

上一篇:Shell脚本实现删除邮件队列

栏    目:其它服务器

下一篇:Shell脚本实现自动安装zookeeper

本文标题:Shell脚本实现批量下载资源并保留原始路径

本文地址:https://zz.feitang.co/server/34849.html

广告投放 | 联系我们 | 版权申明

申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:257218569 | 邮箱:257218569@qq.com

Copyright © 2018-2025 站长天空 版权所有 Powered by EyouCms冀ICP备14023439号