How to add a progress bar to a shell script?

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

    I have built on the answer provided by fearside

    This connects to an Oracle database to retrieve the progress of an RMAN restore.

    #!/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}%%"
    
    }
    
    function rman_check {
    sqlplus -s / as sysdba < sofar
    AND
    opname NOT LIKE '%aggregate%'
    AND
    opname like 'RMAN%';
    exit
    EOF
    }
    
    # Variables
    _start=1
    
    # This accounts as the "totalState" variable for the ProgressBar function
    _end=100
    
    _rman_progress=$(rman_check)
    #echo ${_rman_progress}
    
    # Proof of concept
    #for number in $(seq ${_start} ${_end})
    
    while [ ${_rman_progress} -lt 100 ]
    do
    
    for number in _rman_progress
    do
    sleep 10
    ProgressBar ${number} ${_end}
    done
    
    _rman_progress=$(rman_check)
    
    done
    printf '\nFinished!\n'
    

提交回复
热议问题