[英]tail -f file1 | perl -pe '$_' > file2 does not output anything to file2
This command does not output anything to file2: 此命令不向file2输出任何内容:
#!/bin/bash
echo content > file1
tail -f file1 | perl -pe '$_' > file2
Whereas these commands work ok: 虽然这些命令可以正常工作:
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
Anyone knows what's happening? 谁知道发生了什么?
perl
has detected that stdout is not a terminal. perl
检测到stdout不是终端。 For efficiency, it waits until it has a full block of data to write. 为了提高效率,它会一直等到要写入完整的数据块。 Since you don't provide more data, it won't write anything until
tail
exits and the program can finish. 由于您不提供更多数据,因此在
tail
退出并且程序可以完成之前不会写任何内容。
You can enable autoflushing with $|++
: 您可以使用
$|++
启用autoflushing :
tail -f file1 | perl -pe '$|++; $_' > file2
The one with no output suffers from a combination of buffered output and non-termination. 没有输出的那个遭受缓冲输出和非终止的组合。
tail -f
will never exit, waiting forever for file1
to grow. tail -f
永远不会退出,永远等待file1
增长。 However, the output of perl
is (apparently) buffered, so it waits until it has some minimal amount of output (more than a single line containing "content") to actually write to file2
. 然而,
perl
的输出(显然)是缓冲的,所以它等到它有一些最小量的输出(超过一行包含“内容”)来实际写入file2
。 Until more output is generated, or perl
exits, the output remains unflushed. 在生成更多输出或
perl
退出之前,输出仍保持未刷新状态。
Each of your working examples either terminates, flushing any buffered output at that time; 每个工作示例要么终止,要么刷新当时的任何缓冲输出; or its output is unbuffered, allowing it to appear as it is generated.
或者它的输出是无缓冲的,允许它在生成时显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.