我正在编写一个脚本,根据该脚本,当我运行它时,某些信息将显示在输出屏幕上。
例如说恒定数据显示的是:
my name is mukesh.
i am 27 years old
unix version 7.2.3.0
但除了上面的显示之外,还需要显示其他内容(不同的数据) IE
Process A is starting
Process A is running
Process A is completed.
但我不想要上面的显示。
我想Process A is starting
从屏幕上清除并替换为Process A is running
然后由Process A is completed.
我不太热衷于使用,clear
因为它会删除包含的整个屏幕恒定数据还。而且还因为恒定数据需要花费大量时间来处理并显示在屏幕上。
答案1
您可以清除一行并使用回车符 ( \r
) 到达该行的开头。
clr2eol=`tput el` # capture escape sequence for "clear-to-end-of-line"
echo -n "Process A has started." # display without a newline
sleep 3
echo -n "\r${clr2eol}Process A is running." # move to beginning of line (bol), clear, and display new text
sleep 5
echo -n "\r${clr2eol}Process A has completed." # again, move to bol, clear and display new test
echo # terminating newline you may not want to have this sent right away
阅读联机帮助页terminfo
。
答案2
回车符 ( \r
) 将返回到当前行的开头,因此您可以覆盖文本:
printf "%s\r" "Process A is starting "
sleep 5
printf "%s\r" "Process A is running "
sleep 5
printf "%s\n" "Process A is completed."
答案3
要执行类似的操作,您必须使用类似 (n)curses 的内容:http://www.gnu.org/software/ncurses/