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条回答
小情绪 Triste *
2楼-- · 2020-02-13 04:16

Using tput commands

#!/bin/bash

x=0
load=''

TEST()
{

echo doin stuff


}
tput smcup

while [[ $x -lt 100  ]]; do
        TEST
        load="$load#"



         (( x = x + 1 ))
        echo "loading [" $load "] $x%"
        sleep 0.1
        if [[ $x -eq 100 ]]
        then
                echo done
                read -s LAL
        fi

        tput clear
done
tput clear
tput rmcup
查看更多
贪生不怕死
3楼-- · 2020-02-13 04:24

https://github.com/extensionsapp/progre.sh

Create 24 percent progress: progreSh 24

enter image description here

查看更多
倾城 Initia
4楼-- · 2020-02-13 04:24

There is a lot of options, you may save and restore cursor position like this:

echo -e "\e[s 10 \e[u"
echo -e "\e[s 20 \e[u"

This is one of the vt100 terminal controlling commands, one of the commands in list:

http://www.cse.psu.edu/~kyusun/class/cmpen472/11f/hw/hw7/vt100ansi.htm

or just use \r to move cursor to the beginning of the line and redraw bar each time

echo -e "Original\rOverwrite"

The \r is more portable and will work across most UNIXes (unsure about MacOS)

In your example:

echo -n "["
for ((i = 0 ; i <= 100 ; i+=6)); do
    echo -ne "###\e[s] ($i%)\e[u"
    sleep 0.5
done
echo
查看更多
Rolldiameter
5楼-- · 2020-02-13 04:29

You can use pv as the progress bar:

{
    for ((i = 0 ; i <= 100 ; i+=6)); do
        sleep 0.5 
        echo "B"
    done | pv -c -s 34 > /dev/null
}
查看更多
甜甜的少女心
6楼-- · 2020-02-13 04:31

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 % 
查看更多
放我归山
7楼-- · 2020-02-13 04:31

You can do something like below:

#!/bin/bash

func1() {
#reset
str=`printf '%*s' $1 ''|tr ' ' '#'`
echo -n "Loading Module: [ $str ] $1%Complete" $'\r'
#echo $processCounter
}

processCounter=0

while [  $processCounter -lt 100 ]; do

        #After Operation 1
        sleep 2
        let processCounter=processCounter+15
        func1 $processCounter

        #After operation 2
        sleep 2
        let processCounter=processCounter+35
        func1 $processCounter

done

sleep is Just to simulate the time require by application to install.

Using the above syntax You can do as below

#!/bin/bash
{
    #echo -n "["
    for ((i = 0 ; i <= 100 ; i+=6)); do
        sleep 1
        str=`printf '%*s' $i ''|tr ' ' '#'`
        echo -n "[ $str ] $i %Complete" $'\r'
    done
    echo -n "]"
    echo
}

Hope this helps!!

查看更多
登录 后发表回答