简体   繁体   English

如何在 bash 中覆盖以前的 output

[英]How to overwrite previous output in bash

I have a bash script, that outputs top most CPU intensive processes every second to the terminal.我有一个 bash 脚本,它每秒向终端输出 CPU 密集度最高的进程。

tmp=$(ps -e -eo pid,cmd,%mem,%cpu,user --sort=-%cpu | head -n 11)
printf "\n%s\n" "$tmp[pid]"

I know that I can move my cursor to the predeclared position, but that fails every time terminal is not cleared.我知道我可以将我的 cursor 移动到预先声明的 position,但每次未清除终端时都会失败。 I could also just go to the beginning of the line and write over it, but that again makes a problem when current output is shorter that the previous and when the number of lines is not the same as it was at the previous output.我也可以只将 go 放在行的开头并覆盖它,但是当当前的 output 比之前的更短并且当行数与之前的 Z78E6221F6393D135866 不同时,这又会产生问题。

Is there a way to completely erase the previous output and write from there?有没有办法完全擦除以前的 output 并从那里写入?

Yes, you can clear a part of the screen before each iteration (see https://unix.stackexchange.com/questions/297502/clear-half-of-the-screen-from-the-command-line ), but the function watch does it for you.是的,您可以在每次迭代之前清除屏幕的一部分(请参阅https://unix.stackexchange.com/questions/297502/clear-half-of-the-screen-from-the-command-line ),但是function手表为您完成。 Try:尝试:

watch -n 1 "ps -e -eo pid,cmd,%mem,%cpu,user --sort=-%cpu | head -n 11"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM