Bash 中的退格键
如何将刚刚用 bash 编写的行退格并在其位置上放一个新行?我知道这是可能的,Aptitude (apt-get) 使用它来进行一些更新,而且看起来很棒。
How do you backspace the line you just wrote with bash and put a new one over its spot? I know it's possible, Aptitude (apt-get) use it for some of the updating stuff and it looks great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
如您所见,输出回车符会将光标移动到同一行的开头。
您可以像这样清除该行:
使用
tput
为您提供了一种将控制字符发送到终端的便携式方法。请参阅man 5 terminfo
以获取控制代码列表。通常,您需要将序列保存在变量中,这样就不需要重复调用外部实用程序:Try this:
As you can see, outputting a carriage return moves the cursor to the beginning of the same line.
You can clear the line like this:
Using
tput
gives you a portable way to send control characters to the terminal. Seeman 5 terminfo
for a list of control codes. Typically, you'll want to save the sequence in a variable so you won't need to call an external utility repeatedly:我不太清楚你想要什么,但是,根据你的终端设置,你可以将 ^H (控制 H)打印到屏幕上,这会将光标后退一个位置。
另请注意,某些终端能够将光标移动到行首,在这种情况下,您需要移动到行首,打印足够的空格来覆盖整行(通常可从 $COLUMNS 获得),然后打印任何消息或其他内容。
如果您确切地澄清了您想要的内容并且我可以回答您,我将更新我的答案。
It's not really clear to me what you want, but, depending on your terminal settings you can print ^H (control H) to the screen and that will back the cursor up one position.
Also note that some terminals have the ability to move the cursor to the beginning of the line, in which case you'd move to the beginning of the line, print enough spaces to overwrite the entire line (Usually available from $COLUMNS) and then print any message or whatever.
If you clarify exactly what you want and I can answer you I'll update my answer.
这是使用 find 命令的示例while-read 循环仅在一行上不断打印到 stdout 的完整文件路径:
Here's an example using the find command & a while-read loop to continually print full file paths to stdout on a single line only:
最初的问题是关于动画进度条(在 apt-get 中),但似乎没有人真正回答这个最初的问题。
不管怎样,我不久前根据这个问题的最佳答案写了一些代码 StackOverflow 线程。有许多精美复杂的进度条,但没有一个是“倒计时”或“消失”的进度条。我也没有讨论该条在实际应用程序中可能代表的数据或位置。根据测量的内容,不存在适用于所有用例的单一解决方案。因此,我把这部分问题留给读者。
快速、Bash、无分叉
0.3 秒后
1.8 秒
后 4.6 秒
后等等。
条形可以是任意字符或长度
或
Bash for循环
Bash 参数扩展 给出 字符串变量的长度
Echo 带有抑制换行符标志并解释转义字符标志 -n -e
Bash 子字符串扩展 变量 $i 扩展为字符串中的字符数:第一个 ${bar}
迭代,然后从那里开始倒数。这也可以做到
具有负偏移量。
例如
Bar 可以用其他字符甚至空格替换;回车符“退格”最右边的字符,然后是一个数字并替换一些其他字符或空格,例如连字符“-”
或
可配置计时; sleep 接受整数和浮点数作为输入,因此将其配置为每半秒或一秒的多个小数部分(例如 2.7 秒)运行一次
The original question was for an animated progress bar (in apt-get) but it appears that nobody really answered that original question.
Either way I wrote this a bit of code a little while ago based on the top answer to this StackOverflow Thread. There are many beautifully complicated progress bars, but none of them "count-down" or "disappearing" progress bar. I also did not address the data or placement that this bar would likely represent in a real-world application. Depending what one is measuring, there is no such single solution to apply to all use cases. Thus, I leave that part of the problem to the reader.
Fast, Bash, and no forks
After 0.3s
After 1.8s
After 4.6s
and so on.
Bar Can be any character or length
or
Bash for loop
Bash parameter expansion gives the length of the string variable
Echo with suppress newline flag and interpret escaped chars flag -n -e
Bash Substring Expansion variable $i expands to the number of chars in string: ${bar} on the first
iteration and then counts down from there. This could also be done
with a negative offset.
e.g.
Bar can be replaced by other chars or even whitespace; the carriage return "backspaces" the rightmost char, then a single digit and replace some other char or whitespace, e.g. a hyphen '-'
or
Configurable timing; sleep takes integers and floats as input, so configuring this to run every half-second or multiple fractions of a second like 2.7 seconds