问题描述
在 bash 或 *NIX 中的任何其他 shell 中编写脚本时,在运行需要几秒钟以上的命令时,需要一个进度条.
When scripting in bash or any other shell in *NIX, while running a command that will take more than a few seconds, a progress bar is needed.
比如复制一个大文件,打开一个大的tar文件.
For example, copying a big file, opening a big tar file.
您建议通过哪些方式向 shell 脚本添加进度条?
What ways do you recommend to add progress bars to shell scripts?
推荐答案
您可以通过覆盖一行来实现.使用 回到行首,无需将
写入终端.
You can implement this by overwriting a line. Use to go back to the beginning of the line without writing
to the terminal.
在完成行进后写 .
Write when you're done to advance the line.
使用 echo -ne
来:
- 不打印
和
- 识别转义序列,如
.
- not print
and
- to recognize escape sequences like
.
这是一个演示:
echo -ne '##### (33%)
'
sleep 1
echo -ne '############# (66%)
'
sleep 1
echo -ne '####################### (100%)
'
echo -ne '
'
在下面的评论中,puk 提到如果你从长行开始然后想写一个短行,这个失败":在这种情况下,你需要覆盖长行的长度(例如,用空格).
In a comment below, puk mentions this "fails" if you start with a long line and then want to write a short line: In this case, you'll need to overwrite the length of the long line (e.g., with spaces).
这篇关于如何向shell脚本添加进度条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!