How to: Progress bar in bash

2020-02-13 03:48发布

I want to show a progress bar during a bash operation in a specific format something similar to the following:

[###########](40%)

after update it should become

[###############](50%)

and then similarly reach upto 100%

Is there any way to achieve this

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
}

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.

标签: linux bash
8条回答
Viruses.
2楼-- · 2020-02-13 04:31

A sed based approach:

for((i=1;i<=100;i++)); do
    printf "[%100s] $i%%\r" | sed -r "
          s/ /#/$i;
          h;
          s/.*#//;
          x;
          s/#.*/#/;
          s/ /#/g;
          G;
          s/\n//";
     sleep 0.1
 done;echo

Explanation:

  1. Print string "[<100 spaces here>] progress number%\r"
  2. Replace i'th space with #
  3. Copy to hold space
  4. Split the string by # & store both parts - one in pattern space & one in hold space.
  5. replace all spaces in first part by #
  6. Join both the strings
查看更多
Root(大扎)
3楼-- · 2020-02-13 04:33

Using printf:

for((i=0;i<=100;i+=6)); do
    printf "%-*s" $((i+1)) '[' | tr ' ' '#'
    printf "%*s%3d%%\r"  $((100-i))  "]" "$i"
    sleep 0.1
done; echo

Output: (in same line.. printed on different lines here for demo.)

[                                                                                                   ]  0%
[######                                                                                             ]  6%
[############                                                                                       ] 12%
[##################                                                                                 ] 18%
[########################                                                                           ] 24%
[##############################                                                                     ] 30%
[####################################                                                               ] 36%
[##########################################                                                         ] 42%
[################################################                                                   ] 48%
[######################################################                                             ] 54%
[############################################################                                       ] 60%
[##################################################################                                 ] 66%
[########################################################################                           ] 72%
[##############################################################################                     ] 78%
[####################################################################################               ] 84%
[##########################################################################################         ] 90%
[################################################################################################   ] 96%
查看更多
登录 后发表回答