2016-12-02 29 views
0

tput cuu 1 && tput el适用于多个echo的情况。但是,如何更换由read打印的行呢?如何使用输入覆盖读取打印的行

echo "First line..." 
read -p "Press any key to overwrite this line... " -n1 -s 
tput cuu 1 && tput el 
echo "Second line. read replaced." 

上述输出的例子:

First line... Second line. read replaced.

我想的最终结果为:

First line... Second line. read replaced.

+0

'tput cuu1'将光标向上移动1,但行保持不变。您还需要将光标移动到左侧。 – alvits

+0

@alvits不是'tput el'基本上应该消灭整条线? – Luke

+0

编号'el'从当前位置擦除行尾。 – alvits

回答

2

您的代码光标不移动到第0列。

一个简单的解决方案离子是在保存光标位置之前让read使用tput sc打印提示。

读取用户输入后,您可以使用tput rc恢复光标位置。

你的代码现在应该看起来像这样。

echo "First line..." 
tput sc 
read -p "Press any key to overwrite this line... " -n1 -s 
tput rc 1; tput el 
echo "Second line. read replaced." 

希望这会有所帮助。

+0

非常感谢@alvits,完美的作品! :) – Luke