且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何覆盖Shell脚本中的多行?

更新时间:2023-12-05 14:48:28

我找到了一个解决方案,需要花点时间进行挖掘,但我仍然尚不确定这是如何工作的。但是,看来程序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.