程序源码
# 使用示例: download_file https://pic.cnblogs.com/face/1275166/20180926122056.png
# 参数1:文件URL
download_file () {
url=$1
agent="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11"
file_name=`echo ${url} | awk -F '?' '{print $1}' | awk -F '/' '{print $NF}'`
if [ ! -f ${file_name} ];then
curl ${url} -A "${agent}" -o ${file_name} --progress-bar
fi
}
测试用例
#!/bin/bash
# test.sh
# 使用示例: download_file https://pic.cnblogs.com/face/1275166/20180926122056.png
# 参数1:文件URL
download_file () {
url=$1
agent="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11"
file_name=`echo ${url} | awk -F '?' '{print $1}' | awk -F '/' '{print $NF}'`
if [ ! -f ${file_name} ];then
curl ${url} -A "${agent}" -o ${file_name} --progress-bar
fi
}
# 下载图片
download_file https://pic.cnblogs.com/face/1275166/20180926122056.png
测试结果
$ sh test.sh
######################################################################## 100.0%