How to add a progress bar to a shell script?

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

    I was looking for something more sexy than the selected answer, so did my own script.

    Preview

    Source

    I put it on github progress-bar.sh

    progress-bar() {
      local duration=${1}
    
    
        already_done() { for ((done=0; done<$elapsed; done++)); do printf "▇"; done }
        remaining() { for ((remain=$elapsed; remain<$duration; remain++)); do printf " "; done }
        percentage() { printf "| %s%%" $(( (($elapsed)*100)/($duration)*100/100 )); }
        clean_line() { printf "\r"; }
    
      for (( elapsed=1; elapsed<=$duration; elapsed++ )); do
          already_done; remaining; percentage
          sleep 1
          clean_line
      done
      clean_line
    }
    

    Usage

     progress-bar 100
    

提交回复
热议问题