为什么我这样做时得不到相同的结果: 1.转到我的终端并输入
curl -I www.httpbin.org
结果:
HTTP/1.1 200 OK
Connection: keep-alive
Server: meinheld/0.6.1
Date: Wed, 20 Dec 2017 19:20:56 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 13011
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-Powered-By: Flask
X-Processed-Time: 0.00580310821533
Via: 1.1 vegur
2.创建一个文件APIConnection.sh
包含:
#! /bin/bash
api_url_1="www.httpbin.org"
echo `curl -I $api_url_1`
然后转到我的终端并执行文件:./APIConnection.sh
结果:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 13011 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
Via: 1.1 vegurme: 0.0147929191589 true
在curl
中解释了在反引号之间运行man curl
时仅显示进度条的原因:
进度计
curl normally displays a progress meter during operations, indicating the amount of transferred data, transfer speeds and estimated time left, etc. The progress meter displays number of bytes and the speeds are in bytes per second. The suffixes (k, M, G, T, P) are 1024 based. For example 1k is 1024 bytes. 1M is 1048576 bytes. curl displays this data to the terminal by default, so if you invoke curl to do an operation and it is about to write data to the terminal, it disables the progress meter as otherwise it would mess up the output mixing progress meter and response data.
如果要显式关闭进度表,可以使用--silent
完成。
``
是bash和其他POSIX兼容shell中command substitution的遗留语法。这意味着输出由shell捕获,而不是直接写入终端。因为curl
无法检测到它随后会被echo
写入终端,所以它不会禁用如上所述的进度表。
如果你真的想在这里使用命令替换(而不是让你的脚本运行curl -I "$api_url_1"
而没有任何命令替换或回显),请在命令替换周围加上引号以避免字符串拆分和glob扩展。
建议使用现代命令替换语法$()
,因此每个替换都有自己的引用上下文:
echo "$(curl --silent -I "$api_url_1")"
执行curl -I www.httpbin.org
会在执行echo `curl -I $api_url_1`
时为您提供标准输出的结果,为您提供标准错误的结果。要查看差异,请执行以下命令,然后查看创建的se.txt
和so.txt
文件的内容:
curl -I www.httpbin.org 1>so.txt 2>se.txt
查看about stdin, stdout and stderr了解更多信息。