運用
|&
將第一個命令的標準輸出和標準錯誤連線到第二個命令,而|
僅將第一個命令的標準輸出連線到第二個命令。
在此示例中,頁面通過 curl
下載。與 -v
選項 curl
在 stderr
上寫了一些資訊,包括,下載的頁面寫在 stdout
上。頁面標題可以在 <title>
和 </title>
之間找到。
curl -vs 'http://www.google.com/' |& awk '/Host:/{print} /<title>/{match($0,/<title>(.*)<\/title>/,a);print a[1]}'
輸出是:
> Host: www.google.com
Google
但是對於|
,將會列印更多資訊,即那些被髮送到 stderr
的資訊,因為只有 stdout
被傳送到下一個命令。在此示例中,除最後一行(Google)之外的所有行都由 curl
傳送到 stderr
:
* Hostname was NOT found in DNS cache
* Trying 172.217.20.228...
* Connected to www.google.com (172.217.20.228) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: www.google.com
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Date: Sun, 24 Jul 2016 19:04:59 GMT
< Expires: -1
< Cache-Control: private, max-age=0
< Content-Type: text/html; charset=ISO-8859-1
< P3P: CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."
< Server: gws
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< Set-Cookie: NID=82=jX0yZLPPUE7u13kKNevUCDg8yG9Ze_C03o0IM-EopOSKL0mMITEagIE816G55L2wrTlQwgXkhq4ApFvvYEoaWF-oEoq2T0sBTuQVdsIFULj9b2O8X35O0sAgUnc3a3JnTRBqelMcuS9QkQA; expires=Mon, 23-Jan-2017 19:04:59 GMT; path=/; domain=.google.com; HttpOnly
< Accept-Ranges: none
< Vary: Accept-Encoding
< X-Cache: MISS from jetsib_appliance
< X-Loop-Control: 5.202.190.157 81E4F9836653D5812995BA53992F8065
< Connection: close
<
{ [data not shown]
* Closing connection 0
Google