如何在linux终端上动态显示结果?比如';顶部';命令

如何在linux终端上动态显示结果?比如';顶部';命令,linux,terminal,command,Linux,Terminal,Command,我想实现一个linux命令,从一个可更改的文件中读取 每秒钟在终端中动态显示一次。如何在linux终端上动态显示结果?比如“最高”命令?谢谢 您可以使用man中描述的watch工具(Linux在线man位于man7.org网站上): 例如: watch -n 1 tail -n 23 file 这将每1秒运行一次命令tail-n23 file(显示文件file的最后25行)(选项-n1的watch)watch将运行命令并打印其输出,休眠秒数,然后使用terminal()命令序列清除屏幕。wat

我想实现一个linux命令,从一个可更改的文件中读取
每秒钟在终端中动态显示一次。如何在linux终端上动态显示结果?比如“最高”命令?谢谢

您可以使用man中描述的
watch
工具(Linux在线man位于man7.org网站上):

例如:

watch -n 1 tail -n 23 file
这将每1秒运行一次命令
tail-n23 file
(显示文件
file
的最后25行)(选项
-n1
watch
watch
将运行命令并打印其输出,休眠秒数,然后使用terminal()命令序列清除屏幕。
watch
有几种实现,最简单的是在
busybox
包中:

\033[H
\033[J
序列可以清除屏幕(和fflush\u都是自定义的busybox变体)。Linux在
控制台代码(4)
手册页:;和
\033
是ESC,
ESC[
是CSI,并且
ECMA-48 CSI序列
部分描述了CSI
H
和CSI
J
命令:

   ECMA-48 CSI sequences

   CSI ( or ESC [ ) is followed by a sequence of parameters .. An empty or
   absent parameter is taken to be 0.  The sequence of parameters may be
   preceded by a single question mark.

   H   CUP       Move cursor to the indicated row, column (origin at 1,1).
   J   ED        Erase display (default: from cursor to end of display).

它已经实现了。查找
watch
命令。谢谢@Arash,'watch'可以帮助我。但我想知道'top'和'watch'是如何工作的,这就是它们如何刷新终端中的输出的,也许我应该看看它们的源代码。
while (1) {
    /* home; clear to the end of screen */
    printf("\033[H""\033[J");
...
    fflush_all();
...
    system(cmd);
    sleep(period);
}
   ECMA-48 CSI sequences

   CSI ( or ESC [ ) is followed by a sequence of parameters .. An empty or
   absent parameter is taken to be 0.  The sequence of parameters may be
   preceded by a single question mark.

   H   CUP       Move cursor to the indicated row, column (origin at 1,1).
   J   ED        Erase display (default: from cursor to end of display).