shell-回声某些命令(curl)与终端中的命令键入命令



为什么当我这样做时我不会得到相同的结果:
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

man curl中解释了在Backticks之间运行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的外壳中命令替换的旧语法。这意味着输出是由外壳捕获的,而不是直接写入终端。因为curl无法检测到以后通过echo将其写入终端,因此不会如上所述禁用进度仪表。

如果您确实想在此处使用命令替换(而不是仅仅让您的脚本运行curl -I "$api_url_1"而无需任何命令替代 echo),请在命令替换周围添加引号,以避免使用字符串 - 地球扩展。

也建议使用现代命令替代语法$(),因此每个替代都有其自己的引号:

echo "$(curl --silent -I "$api_url_1")"

执行curl -I www.httpbin.org为您提供标准输出的结果,同时执行echo `curl -I $api_url_1`为您提供标准错误的结果。要查看差异执行以下命令,然后查看创建的se.txtso.txt文件的内容:

 curl -I www.httpbin.org 1>so.txt 2>se.txt

查看有关stdin,stdout和stderr的更多信息。

最新更新