简体   繁体   English

如何将 bash 脚本中的多行输出覆盖到终端中

[英]How to overwrite multiple outputted lines from a bash script into the terminal

I have a kubernetes command kubectl get pods which prints to the terminal this output我有一个 kubernetes 命令kubectl get pods将这个输出打印到终端

 NAME                          READY   STATUS    RESTARTS   AGE
redis-cart-74594bd569-ffrhk   1/1     Running   0          40m

I want this command to run repeatedly every two seconds in a bash script.我希望此命令在 bash 脚本中每两秒重复运行一次。 However, I want the output to overwrite the last input.但是,我希望输出覆盖最后一个输入。

Currently, I can get it to overwrite one line (the first outputted line) by using a carriage return.目前,我可以通过使用回车来覆盖一行(第一行输出)。 But it doesn't overwrite the second line.但它不会覆盖第二行。

How can I overwrite previously outputted text from my bash script?如何覆盖以前从我的 bash 脚本输出的文本?

By script looks like this atm通过脚本看起来像这个 atm

#!/bin/bash

variable=$(kubectl get pods)

while true
do
    variable=$(kubectl get pods)
    echo -ne "$variable" "\r"
    sleep 2
done
exit

Add clear command to the loop:clear命令添加到循环中:

#!/bin/bash

variable=$(kubectl get pods)

while true
do
    variable=$(kubectl get pods)
    clear
    echo -ne "$variable" "\r"
    sleep 2
done

But, you can use watch or -w to get a similar result without a special script.但是,您可以使用watch-w在没有特殊脚本的情况下获得类似的结果。

try watch to refresh output ...尝试watch刷新输出...

watch kubectl get pods

To end the process you can use CTRL+C or look for the process via ps or pgrep and send a SIGINT like pgrep watch kubectl | xargs kill -1要结束该进程,您可以使用CTRL+C或通过pspgrep查找进程并发送SIGINT,pgrep watch kubectl | xargs kill -1 pgrep watch kubectl | xargs kill -1 . pgrep watch kubectl | xargs kill -1

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

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