2016-12-02 34 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