2011-09-08 50 views
1

我試圖改變終端中的回顯行的文本,\ r適用於我,但不是當我把它放在.sh文件中。我想運行一個.sh文件,以1秒爲間隔更改回顯行(不要問爲什麼)。當我做如何在迴音線上書寫?

echo -ne 'hello\r' 
sleep 1 
echo -ne 'bye\r' 

-ne hello 
-ne bye 

輸出我怎麼acheive我的目標是什麼?

+0

你是否有機會使用busybox?看起來你有一個不兼容的回聲。另外,你的例子應該是\ r不是\ n。 –

+0

哦是的,我的意思是\ r,我會修復 – Zack

回答

3

使用printf,這是規範,而不是-ne選項echo,這是不。

printf '\033[K%s\r' "hello world" 
sleep 1 
printf '\033[K%s\r' "bye now" 

確保您在退出時打印換行符,例如:與

trap 'echo' 0 

之前的第一個printf命令。

+0

並沒有刪除那裏有什麼 – Zack

+0

啊,對,我忘了'\ r'不這樣做。您需要@chown指出的終端控制代碼之一。我糾正了我的答案。 – zwol

+0

所以把整個腳本之前的陷阱'回聲'0? – Zack

3

Cursor Movement

的重要部分:

- Position the Cursor: 
    \033[<L>;<C>H 
    Or 
    \033[<L>;<C>f 
    puts the cursor at line L and column C. 
- Move the cursor up N lines: 
    \033[<N>A 
- Move the cursor down N lines: 
    \033[<N>B 
- Move the cursor forward N columns: 
    \033[<N>C 
- Move the cursor backward N columns: 
    \033[<N>D 

- Clear the screen, move to (0,0): 
    \033[2J 
- Erase to end of line: 
    \033[K 

- Save cursor position: 
    \033[s 
- Restore cursor position: 
    \033[u 

所以像:

echo -ne "\033[50D" 
echo -ne "\033[K"