How to add a progress bar to a shell script?

后端 未结 30 2712
情歌与酒
情歌与酒 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:26

    Got an easy progress bar function that i wrote the other day:

    #!/bin/bash
    # 1. Create ProgressBar function
    # 1.1 Input is currentState($1) and totalState($2)
    function ProgressBar {
    # Process data
        let _progress=(${1}*100/${2}*100)/100
        let _done=(${_progress}*4)/10
        let _left=40-$_done
    # Build progressbar string lengths
        _fill=$(printf "%${_done}s")
        _empty=$(printf "%${_left}s")
    
    # 1.2 Build progressbar strings and print the ProgressBar line
    # 1.2.1 Output example:                           
    # 1.2.1.1 Progress : [########################################] 100%
    printf "\rProgress : [${_fill// /#}${_empty// /-}] ${_progress}%%"
    
    }
    
    # Variables
    _start=1
    
    # This accounts as the "totalState" variable for the ProgressBar function
    _end=100
    
    # Proof of concept
    for number in $(seq ${_start} ${_end})
    do
        sleep 0.1
        ProgressBar ${number} ${_end}
    done
    printf '\nFinished!\n'
    

    Or snag it from,
    https://github.com/fearside/ProgressBar/

提交回复
热议问题