作法:bash中的进度列 [英] How to: Progress bar in bash
问题描述
我想在bash操作期间以特定格式显示进度条,类似于以下内容:
I want to show a progress bar during a bash operation in a specific format something similar to the following:
[###########](40%)
更新后应该变成
[###############](50%)
,然后类似地达到100%
有什么办法可以实现这一目标
Is there any way to achieve this
我编写了以下bash程序,但我不知道如何在同一行中显示百分比:
I wrote the following bash program but i don't know how to show the percentage in this on same line:
#!/bin/bash
{
echo -n "["
for ((i = 0 ; i <= 100 ; i+=6)); do
sleep 0.05
echo -n "###"
done
echo -n "]"
echo
}
让我们假设某个循环中正在执行一定数量的操作,并且在完成每个操作之后,我想报告一些进度.我不想安装pv
或默认Ubuntu 12.04
安装未附带的任何其他实用程序.
Let's assume that a certain number of operations are being performed in a loop and after the completion of each operation, I want to report some progress. I don't want to install pv
or any other utilities that does not come with default Ubuntu 12.04
installation.
推荐答案
让我们使用echo -n '...' $'\r'
打印回车符:
Let's use echo -n '...' $'\r'
to print a carriage return:
for ((k = 0; k <= 10 ; k++))
do
echo -n "[ "
for ((i = 0 ; i <= k; i++)); do echo -n "###"; done
for ((j = i ; j <= 10 ; j++)); do echo -n " "; done
v=$((k * 10))
echo -n " ] "
echo -n "$v %" $'\r'
sleep 0.05
done
echo
它使光标移动到行的开头以继续打印.
It makes the cursor move to the beginning of the line to keep printing.
输出如下,始终在同一行:
Output is as follows, always in the same line:
[ ################## ] 50 %
.../...
[ ################################# ] 100 %
这篇关于作法:bash中的进度列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!