如何覆盖Shell脚本中的多行? [英] How do I overwrite multiple lines in a shell script?

查看:188
本文介绍了如何覆盖Shell脚本中的多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一遍又一遍地写多行到终端。像这样的

I want to write multiple lines over and over to the terminal. Something like

echo "One Line"
echo "Two Lines"
echo "\r\b\rThree Lines"
echo "Four Lines"

理想情况下,这将首先输出:

Ideally this would first output:

One Line
Two Lines

然后此输出将替换为

Three Lines
Four Lines

麻烦的是,回车符会让您覆盖一行输出,但是您可以用withb越过then。然后,我该如何覆盖多行?

Trouble is, while the carriage return will let you overwrite one line of output, you can't get past the \n with a \b. How do I then overwrite multiple lines?

推荐答案

我找到了一个解决方案,需要花点时间进行挖掘,但我仍然尚不确定这是如何工作的。但是,看来程序tput将允许您获取特殊字符以清除行和定位光标。具体来说, tput el 将清除到当前行的开头(而不只是重新定位光标)。方便地, tput cuu1 会将光标向上移动一行。因此,如果在您的bash脚本中声明了变量,例如:

I found a solution for this one that took a little digging and I'm still not entirely sure on how this one works. However, it seems the program tput will allow you to get special characters for clearing lines and position the cursor. Specifically, tput el will clear to the beginning of the current line (instead of just repositioning the cursor). Conveniently, tput cuu1 will move the cursor up one line. So if in your bash script you declare variables like:

UPLINE=$(tput cuu1)
ERASELINE=$(tput el)

然后您可以编写如下脚本:

You could then write a script like so:

UPLINE=$(tput cuu1)
ERASELINE=$(tput el)
echo "One Line"
echo "Two Lines"
echo "$UPLINE$ERASELINE$UPLINE$ERASELINE\c"
echo "Three Lines"
echo "Four Lines"

,您将获得所需的输出。

and you'll get the desired output.

这篇关于如何覆盖Shell脚本中的多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆