tail -f file1 | perl -pe'$ _'> file2不向file2输出任何内容

vib*_*ibl 2 bash perl buffer

此命令不向file2输出任何内容:

#!/bin/bash
echo content > file1
tail -f file1 | perl -pe '$_' > file2
Run Code Online (Sandbox Code Playgroud)

虽然这些命令可以正常工作:

tail -f file1 > file2
tail -f file1 | perl -pe '$_'
tail file1 | perl -pe '$_' > file2
tail -f /tmp/file1 | while read line; do echo $line | perl -pe '$_' > /tmp/file2 ; done
Run Code Online (Sandbox Code Playgroud)

谁知道发生了什么?

tha*_*guy 5

perl检测到stdout不是终端.为了提高效率,它会一直等到要写入完整的数据块.由于您不提供更多数据,因此在tail退出和程序完成之前不会写任何内容.

您可以启用autoflushing$|++:

tail -f file1 | perl -pe '$|++; $_' > file2
Run Code Online (Sandbox Code Playgroud)