How to add a progress bar to a shell script?

后端 未结 30 2282
情歌与酒
情歌与酒 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条回答
  •  -上瘾入骨i
    2020-11-22 06:37

    Many answers describe writing your own commands for printing out '\r' + $some_sort_of_progress_msg. The problem sometimes is that printing out hundreds of these updates per second will slow down the process.

    However, if any of your processes produce output (eg 7z a -r newZipFile myFolder will output each filename as it compresses it) then a simpler, fast, painless and customisable solution exists.

    Install the python module tqdm.

    $ sudo pip install tqdm
    $ # now have fun
    $ 7z a -r -bd newZipFile myFolder | tqdm >> /dev/null
    $ # if we know the expected total, we can have a bar!
    $ 7z a -r -bd newZipFile myFolder | grep -o Compressing | tqdm --total $(find myFolder -type f | wc -l) >> /dev/null
    

    Help: tqdm -h. An example using more options:

    $ find / -name '*.py' -exec cat \{} \; | tqdm --unit loc --unit_scale True | wc -l
    

    As a bonus you can also use tqdm to wrap iterables in python code.

    https://github.com/tqdm/tqdm/blob/master/README.rst#module

提交回复
热议问题