前言

在写复杂脚本时,往往需要复用下载这个操作,刚好碰到这个问题,就把下载功能抽成一个函数,拿来复用。

实现

实现就比较简单了,直接上代码,这段代码复制就可以直接使用。
功能上判断一下当前系统中使用的的wget还是curl,选择其中一个工具进行下载。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash

download() {
local url=$1
local file_name=$2
if type wget >/dev/null 2>&1; then
wget --no-check-certificate -q $url
elif type curl >/dev/null 2>&1; then
echo "curl -OLJ $url"
curl -OLJ $url
else
echo 'info: no exists wget or curl, make sure the system can use the "wget" or "curl" command'
fi
}

// 函数调用测试
test() {
download https://liukay.com/atom.xml localFileName.xml
}

//test

使用

简单的示例,如果需要函数调用函数,直接在上面放开test这个注释即可。

download.sh https://liukay.com/atom.xml localFileName.xml