在以前看过的一篇“对话 Unix操作系统” 中推荐使用 wget 从命令行直接下载文件。在Unix操作系统中在无法使用 Web 浏览器的情况下,在 Unix操作系统shell 脚本中使用 wget 是非常合适的。例如,如果要在远程服务器上安装新软件,wget 确实可以节省时间。
如果您喜欢 wget,也一定会喜欢 cURL。与 wget 一样,cURL 可以下载文件,但是它还可以向 Web 页面表单提交数据、通过 File Transfer Protocol (FTP) 上传文件、作为代理、设置 Hypertext Transfer Protocol (HTTP) 头等等。cURL 在许多方面可以作为浏览器和其他客户机的命令行替代品。因此,它有许多潜在的应用。
通过Unix操作系统常用的 ./configure && make && sudo make install 过程构建 cURL 实用程序。下载、解压并处理:
复制
$ wget http://curl.haxx.se/download/curl-7.19.4.tar.gz $ tar xzf curl-7.19.4.tar.gz $ cd curl-7.19.4 $ ./configure && make && sudo make install
1.
2.
3.
4.
cURL 实用程序有很多选项,***通读它的手册页。下面是一些常见的 cURL 用法:
为了下载一个文件(比如 cURL tarball 本身),使用:
$ curl -o curl.tgz http://curl.haxx.se/download/curl-7.19.4.tar.gz
与 wget 不同,cURL 把它下载的东西发送到 stdout。使用 -o 选项把下载的东西保存到指定的文件。
为了下载大量文件,可以提供序列、集或同时提供这两者。序列 是放在方括号([])中的一个数字范围;集 是放在花括号({})中的逗号分隔的列表。例如,下面的命令从目录 archive1996/vol1 到 archive1999/vol4(含)下载名为 parta.html、partb.html 和 partc.html 的所有文件,共有 48 个文件。
复制
$ curl http://any.org/archive[1996-1999]/vol[1-4]/part{a,b,c}.html \ -o "archive#1_vol#2_part#3.html"
1.
2.
在指定序列或集时,可以提供 -o 选项和一个模板,模板中的 #1 替换为***个序列或集的当前值,#2 是第二个序列或集的占位符,以此类推。另外,还可以提供 -O 选项以保持每个文件名不变。
为了把一组图像上传到Unix操作系统服务器,可以使用 -T 选项:
$ curl -T "img[1-1000].png" ftp://ftp.example.com/upload/
在这里,把 img[1-1000].png 放在引号中,以避免 shell 解释它。这个命令把 img1.png 到 img1000.png 上传到指定的服务器和路径。
Unix操作系统甚至可以用 cURL 在词典中查找单词:
复制
$ curl dict://dict.org/d:stalwart 220 miranda.org dictd 1.9.15/rf on Linux 2.6.26-bpo.1-686 <auth.mime> <400549.18119.1238445667@miranda.org> 250 ok 150 1 definitions retrieved 151 "Stalwart" gcide "The Collaborative International Dictionary of English v.0.48" Stalwart \Stal"wart\ (st[o^]l"w[~e]rt or st[add]l"-; 277), Stalworth \Stal"worth\ (-w[~e]rth), a. [OE. stalworth, AS. staelwyr[eth] serviceable, probably originally, good at stealing, or worth stealing or taking, and afterwards extended to other causes of estimation. See {Steal}, v. t., {Worth}, a.] Brave; bold; strong; redoubted; daring; vehement; violent. "A stalwart tiller of the soil." --Prof. Wilson. [1913 Webster] Fair man he was and wise, stalworth and bold. --R. of Brunne. [1913 Webster] Note: Stalworth is now disused, or but little used, stalwart having taken its place. [1913 Webster] . 250 ok [d/m/c = 1/0/20; 0.000r 0.000u 0.000s] 221 bye [d/m/c = 0/0/0; 0.000r 0.000u 0.000s]
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
把单词 stalwart 替换为您要查找的单词。
除了通过命令行使用之外,还可以通过 Unix操作系统libcurl 库使用 cURL 的所有功能。许多编程语言包含 libcurl 的接口,可以自动执行通过 FTP 传输文件等任务。例如,下面的 PHP 片段使用 libcurl 把通过表单上传的文件存放到 FTP 服务器上:
复制
php ... $ch = curl_init(); $localfile = $_FILES['upload']['tmp_name']; $fp = fopen($localfile, 'r'); curl_setopt($ch, CURLOPT_URL, 'ftp://ftp_login:password@ftp.domain.com/'.$_FILES['upload']['name']); curl_setopt($ch, CURLOPT_UPLOAD, 1); curl_setopt($ch, CURLOPT_INFILE, $fp); curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile)); curl_exec ($ch); $error_no = curl_errno($ch); curl_close ($ch); ... ?>
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
如果必须自动执行任何 Web 访问,可以考虑使用 cURL。大家可以看出Unix操作系统中,使用 cURL 在 Internet 上做各种事情。这给我们带来了很大的方便。让我们一起运用 cURL吧。
【编辑推荐】