パイプを使用して標準出力を処理することはできません。

パイプを使用して標準出力を処理することはできません。

私はfifoでtsharkを実行しています。以下は、tshark出力を印刷するループの簡単な例です。それだけです。:

tshark -i $fifo | while read line; do
    echo $line
done

tsharkにフィルタを追加すると問題が発生します。この例では、$linetsharkが終了した後にのみすべてのsを印刷します(IPアドレスは非表示)。

tshark -i $fifo -T fields -e text -R '
    ip.src == **.**.***.**          &&
    http.response.code == 200       &&
    http.content_encoding == "gzip" &&
    http.content_type contains "text/html"
' | while read line; do
    echo $line
done

別の形式を試しましたが、運がありません。

while read line; do
    echo $line
done < <(tshark ...)

while read line; do
    echo $line
done <<<"$(tshark ...)"

grepもtsharkが終了した後にのみ、次の行を印刷します。

tshark ... | grep .

パイプなしでtsharkを実行してみたところ、線が現れるように正しく印刷されました。 tsharkが終了した後にのみパイプラインの背後にあるコマンドが送信されるのはなぜですか?

追加の詳細: | teeうまくいきますが、tsharkが終了するとすべてを再印刷するので、良い取引ではありません。

ベストアンサー1

stdbufcoreutilsで利用可能です。パイプラインの後の各コマンドもバッファを調整する必要があります。

stdbuf -o 0 tshark -i $fifo -T fields -e text -R '
    ip.src == **.**.***.**          &&
    http.response.code == 200       &&
    http.content_encoding == "gzip" &&
    http.content_type contains "text/html"
' | 
stdbuf -o 0 sed 's/\\r\\n,\?/\n/g; s/\\t/\t/g' |

マニュアルページから:

`stdbuf': Run a command with modified I/O stream buffering

(...)

`-o MODE'
`--output=MODE'
     Adjust the standard output stream buffering.

おすすめ記事