在bash脚本中循环替换输出

时间:2023-01-01 15:43:16

I have a few little scripts that I use for monitoring some aspects of our application that reside in an infinite loop with a 3 second sleep and simply display the output of ps aux | grep -i app_name or mysql -e "SHOW SLAVE STATUS\G" just so I can easily watch for a few mins without hitting any keys, what I'd like to know is how I could 'refresh' the values on the screen instead of appending to them, I guess the best example I have is of top, it refreshes values instead of continuously printing to the screen.

我有几个小脚本用于监视我们的应用程序的某些方面,它们处于无限循环中,具有3秒的休眠时间,并且只显示ps aux |的输出。 grep -i app_name或mysql -e“SHOW SLAVE STATUS \ G”只是这样我可以轻松地观看几分钟但没有按任何键,我想知道的是我如何“刷新”屏幕上的值而不是附加到它们,我想我最好的例子是顶部,它刷新值而不是连续打印到屏幕上。

Any help would be extremely appreciated.

任何帮助将非常感激。

Thanks

4 个解决方案

#1


you can include the "clear" command in your loop (without looking at your code)

你可以在你的循环中包含“清除”命令(不看你的代码)

#2


Try watch utility. It runs arbitrary command in configurable interval (-n flag). It also can highlight differences in output between refreshes (-d flag).

试试看实用程序。它在可配置的间隔(-n标志)中运行任意命令。它还可以突出显示刷新之间输出的差异(-d标志)。

#3


You can also use echo -n -e "\b" to undo your previous output together with echo -n "output" to maintain your new output on the cleared line

您也可以使用echo -n -e“\ b”来撤消先前的输出以及echo -n“output”以在清除的行上保持新输出

#4


The probably simplest way:

可能最简单的方法:

watch -n 3 'ps aux | grep -i app_name'

#1


you can include the "clear" command in your loop (without looking at your code)

你可以在你的循环中包含“清除”命令(不看你的代码)

#2


Try watch utility. It runs arbitrary command in configurable interval (-n flag). It also can highlight differences in output between refreshes (-d flag).

试试看实用程序。它在可配置的间隔(-n标志)中运行任意命令。它还可以突出显示刷新之间输出的差异(-d标志)。

#3


You can also use echo -n -e "\b" to undo your previous output together with echo -n "output" to maintain your new output on the cleared line

您也可以使用echo -n -e“\ b”来撤消先前的输出以及echo -n“output”以在清除的行上保持新输出

#4


The probably simplest way:

可能最简单的方法:

watch -n 3 'ps aux | grep -i app_name'