LoginSignup
1
1

More than 3 years have passed since last update.

tee コマンドを使って 画面出力しつつ pipe で別コマンドにも渡す

Last updated at Posted at 2020-05-05

:warning: Git Bash (mintty) で試しましたが、他の環境では 異なる結果を確認しました(調査中)


tee コマンドで /dev/stderr に出力することで画面にも出るようにできる。

$ cat sep.txt
----
$
$ echo hoge | tee /dev/stderr | cat sep.txt - sep.txt
hoge
----
hoge
----
$
$ echo hoge | tee /dev/stdout | cat sep.txt - sep.txt
----
hoge
hoge
----
$

/dev/stdout を使うと、両方 pipe されて cat に渡る点に注意

実用例

curl telnet に HTTPリクエストを送りつつ画面にも出力したいとき

$ echo -e "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n" | tee /dev/stderr | curl -s telnet://example.com:80
GET / HTTP/1.1
Host: example.com


HTTP/1.1 200 OK
Age: 250082
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Tue, 05 May 2020 14:10:05 GMT
Etag: "3147526947+ident"
Expires: Tue, 12 May 2020 14:10:05 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECS (sjc/4E76)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1256

<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
(後略)

前半の

GET / HTTP/1.1
Host: example.com


の部分が tee /dev/stderr からの標準エラー出力

後半の

HTTP/1.1 200 OK
Accept-Ranges: bytes
Age: 433530
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Tue, 05 May 2020 14:02:02 GMT
Etag: "3147526947+ident"
Expires: Tue, 12 May 2020 14:02:02 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECS (sjc/4E74)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1256

<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
(後略)

の部分が curl からの標準出力

注意点

画面出力に標準エラー出力を使って問題ないか仕様や制約を要確認

参考

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1