每次返回新输出时将应用程序输出的最后一行重定向到文件

Redirecting the last line of an application's output to a file every time a new output is being returned

我试图仅将 运行 应用程序输出的最后一行存储在文件中,每次应用程序 returns 将新输出替换为值时替换文件内容输出的最后一条消息。例如,如果 运行 应用程序 helloworld 产生以下输出:

Message1
Message2
Message3
Message4

而我是运行应用程序:

$ ./helloworld > output.txt

我希望 output.txt 文件始终存储最后一条消息,因此它将首先包含:

Message1

那么当应用returns Message2文件的内容会是:

Message2

等等。文件内容基本上在任何给定时间都只有一行。

写入从 stdin 读取并写入输出文件的循环。

helloworld | while read i; do echo $i > output.txt; done