管道 sed/grep 输出不起作用

Chr*_*ris 1 grep bash pipe sed io-redirection

如果我运行下面的命令,只有out1输出,out2out3为空。

# this is just to generate a self-signed certificate
openssl genrsa -out /tmp/ssl.key 2048
openssl req -sha256 -new -key /tmp/ssl.key -out /tmp/ssl.csr -subj /CN=localhost
openssl x509 -req -days 365 -in /tmp/ssl.csr -signkey /tmp/ssl.key -out /tmp/ssl.crt

# works
openssl s_server -cert /tmp/ssl.crt -key /tmp/ssl.key -accept 444 > out1
# does not work, but if I run without '> out2' it works
openssl s_server -cert /tmp/ssl.crt -key /tmp/ssl.key -accept 446 | sed "s/ACCEPT/ACCEPT445/g" > out2
# does not work, but if I run without '> out3' it works
openssl s_server -cert /tmp/ssl.crt -key /tmp/ssl.key -accept 447 | grep ACCEPT > out3
Run Code Online (Sandbox Code Playgroud)

为什么从 sed 或 grep 重定向 stdout 会失败,但在不重定向的情况下运行却可以?

Gui*_*ido 5

尝试sed -u-l在 BSD/Mac OSX 系统上)和grep --line-buffered选项。