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 1 year has passed since last update.

while read(ループ)へパイプライン出力を渡す(シェル)

Last updated at Posted at 2023-01-12

こんにちは。
while read(ループ)へパイプライン出力を渡して処理しました。

$ printf "a\n\nb\n" | while read -r line; do echo "$line"; done
a

b
$

空行に対して特別な処理を行いたい場合

  • 空行を処理しない場合は、
$ printf "a\n\nb\n" | while read -r line; do [ -n "$line" ] && echo "$line"; done
$ printf "a\n\nb\n" | while read -r line && [ -n "$line" ]; do echo "$line"; done
a
b
$
  • 空行ならば while ループを打ち切る場合は、
$ printf "a\n\nb\n" | while read -r line; do [ -n "$line" ] && echo "$line" || exit; done
a
$
0
0
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
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?