[英]how to make a counter in linux?
I'm trying to write a script where I want to make a countdown of three seconds (probably with the sleep command), but every second I want to display the text "break". 我正在尝试编写一个脚本,在该脚本中我想进行三秒钟的倒计时(可能使用sleep命令),但是我想每秒钟显示一次文本“ break”。
After every second, I want to display a countdown next to the word "break", but the line of text should stay in place. 每秒之后,我想在“ break”一词旁边显示一个倒数,但文本行应保留在原位。 The word "break" must stay in one place;
“休息”一词必须留在一个地方; only the number should change.
只有数字应该改变。
You can use ANSI terminal control sequences . 您可以使用ANSI终端控制序列 。 Like this:
像这样:
# Save the cursor position
echo -en "\033[s";
for i in {3..1} ; do
echo -n "break $i"
sleep 1
# Restore the cursor position
echo -en "\033[u";
done
# Print newline at the end
echo
If you want the last break 1
to disappear from screen then change the last line to 如果您希望最后一个
break 1
从屏幕上消失,则将最后一行更改为
# Clear the line at the end
echo -en "\033[K"
In bash, you can use a for loop, and the character \\r
to move the cursor back the the beginning of the line: 在bash中,可以使用for循环,并使用字符
\\r
将光标移回行的开头:
for ((i=3; i; --i)) ; do
printf 'break %d\r' $i
sleep 1
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.