0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

シェルスクリプト パイプライン

0
Posted at

パイプラインは、一つ以上のコマンドをパイプで区切って並べたもの。
左側のコマンドの標準出力は、右側のコマンドの標準入力になる。

以下のようなテキストファイルがあったとする。

file1.txt
This is file1.

最も右側のコマンドの終了ステータスが、パイプラインの終了ステータスになる。

pipeline1
$ cat file1.txt | grep -q 'file1'
$ echo $?
0
pipeline2
$ cat file1.txt | grep -q 'file2'
$ echo $?
1

否定演算!を付けると、終了ステータスは逆になる。

pipeline3
$ ! cat file1.txt | grep -q 'file2'
$ echo $?
0

右側のコマンド以外の終了ステータスを得るには、PIPESTATUS を参照する。

pipeline4
$ (exit 0) | (exit 1) | (exit 2)
$ echo ${PIPESTATUS[@]}
0 1 2

標準エラー出力もパイプに通す場合。

pipeline5
$ make 2>&1 | tee test.log
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?