如何在 linux 终端动态显示结果?喜欢 'top' 命令
how to show result dynamically in linux terminal? like 'top' command
我想实现一个 linux 命令,该命令从一个可变文件中读取并
每隔一秒动态地在终端中显示一次。如何在 linux 终端动态显示结果?喜欢 'top' 命令?谢谢!
您可以像手册中描述的那样使用 watch
工具(Linux 在线手册在 man7.org 站点上):http://man7.org/linux/man-pages/man1/watch.1.html
watch runs command repeatedly, displaying its output and errors (the
first screenfull). This allows you to watch the program output
change over time. By default, command is run every 2 seconds and
watch will run until interrupted.
例如:
watch -n 1 tail -n 23 file
这将每 1 秒 运行 命令 tail -n 23 file
(显示文件 file
的最后 25 行)(watch
的选项 -n 1
)。 watch
将 运行 命令并打印其输出,休眠几秒,然后使用终端清屏 (ANSI) command sequence. There are several implementations of watch
, and the simplest is in busybox
package: https://git.busybox.net/busybox/tree/procps/watch.c?h=1_17_stable
while (1) {
/* home; clear to the end of screen */
printf("3[H""3[J");
...
fflush_all();
...
system(cmd);
sleep(period);
}
清屏有3[H
和3[J
顺序(fflush_all为just custom busybox variant of fflush(stdout)
). Linux documents such codes in console_codes (4)
man page: http://man7.org/linux/man-pages/man4/console_codes.4.html;3
为ESC,ESC [
是 CSI,ECMA-48 CSI sequences
部分描述了 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).
我想实现一个 linux 命令,该命令从一个可变文件中读取并 每隔一秒动态地在终端中显示一次。如何在 linux 终端动态显示结果?喜欢 'top' 命令?谢谢!
您可以像手册中描述的那样使用 watch
工具(Linux 在线手册在 man7.org 站点上):http://man7.org/linux/man-pages/man1/watch.1.html
watch runs command repeatedly, displaying its output and errors (the
first screenfull). This allows you to watch the program output
change over time. By default, command is run every 2 seconds and
watch will run until interrupted.
例如:
watch -n 1 tail -n 23 file
这将每 1 秒 运行 命令 tail -n 23 file
(显示文件 file
的最后 25 行)(watch
的选项 -n 1
)。 watch
将 运行 命令并打印其输出,休眠几秒,然后使用终端清屏 (ANSI) command sequence. There are several implementations of watch
, and the simplest is in busybox
package: https://git.busybox.net/busybox/tree/procps/watch.c?h=1_17_stable
while (1) {
/* home; clear to the end of screen */
printf("3[H""3[J");
...
fflush_all();
...
system(cmd);
sleep(period);
}
清屏有3[H
和3[J
顺序(fflush_all为just custom busybox variant of fflush(stdout)
). Linux documents such codes in console_codes (4)
man page: http://man7.org/linux/man-pages/man4/console_codes.4.html;3
为ESC,ESC [
是 CSI,ECMA-48 CSI sequences
部分描述了 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).