[英]How to update text of a specific line on terminal?
I'm programming bash script but there is a problem that is how to update text of a specific line.我正在编写 bash 脚本,但有一个问题是如何更新特定行的文本。
I've tried using clear command.我试过使用clear命令。 But using clear is refresh all lines on terminal but i want to refresh specific line.
但是使用clear是刷新终端上的所有行,但我想刷新特定行。 Like under
喜欢下
=============== ================
TIME: 20:35时间:20:35
=============== ================
I want to refresh only "20:35" part, without "=====" and "TIME:".我只想刷新“20:35”部分,没有“=====”和“TIME:”。
1) 1)
while true
do
clear
echo "
===============
TIME: $(date +%H:%M)
==============="
done
2) 2)
function TIME_RE(){
while true
do
printf "TIME: $(date +%Y.%m.%d) ($(date +%H:%M:%S)) \r"
done
}
echo "
===============
TIME: $(TIME_RE)
==============="
I expected result of second is refreshing only " $(TIME_RE) " part, but it displayed nothing.我预期的第二个结果是仅刷新“ $(TIME_RE) ”部分,但它什么也没显示。
You can use ANSI escape codes to move cursor position, or save and restore cursor position.您可以使用ANSI 转义码来移动光标位置,或保存和恢复光标位置。 For example, using the cursor up sequence:
例如,使用光标向上序列:
while true; do
echo -e "
===============
TIME: $(date +%Y.%m.%d) ($(date +%H:%M:%S))
===============
\e[5A"
sleep 1
done
Notes:笔记:
you need echo's -e
option to let you print escape sequences.您需要使用 echo 的
-e
选项来打印转义序列。
the "\\e[5A" is the sequence to move 5 lines up. "\\e[5A" 是向上移动 5 行的序列。
add something like "sleep 1" as delays to avoid burdening the system.添加诸如“sleep 1”之类的内容作为延迟以避免给系统带来负担。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.