How to add a progress bar to a shell script?

后端 未结 30 2664
情歌与酒
情歌与酒 2020-11-22 05:48

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.

For example, copying a b

30条回答
  •  余生分开走
    2020-11-22 06:44

    #!/bin/bash
    
    function progress_bar() {
        bar=""
        total=10
        [[ -z $1 ]] && input=0 || input=${1}
        x="##"
       for i in `seq 1 10`; do
            if [ $i -le $input ] ;then
                bar=$bar$x
            else
                bar="$bar  "
           fi
        done
        #pct=$((200*$input/$total % 2 + 100*$input/$total))
        pct=$(($input*10))
        echo -ne "Progress : [ ${bar} ] (${pct}%) \r"    
        sleep 1
        if [ $input -eq 10 ] ;then
            echo -ne '\n'
        fi
    
    }
    

    could create a function that draws this on a scale say 1-10 for the number of bars :

    progress_bar 1
    echo "doing something ..."
    progress_bar 2
    echo "doing something ..."
    progress_bar 3
    echo "doing something ..."
    progress_bar 8
    echo "doing something ..."
    progress_bar 10
    

提交回复
热议问题