How to add a progress bar to a shell script?

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

    Some posts have showed how to display the command's progress. In order to calculate it, you'll need to see how much you've progressed. On BSD systems some commands, such as dd(1), accept a SIGINFO signal, and will report their progress. On Linux systems some commands will respond similarly to SIGUSR1. If this facility is available, you can pipe your input through dd to monitor the number of bytes processed.

    Alternatively, you can use lsof to obtain the offset of the file's read pointer, and thereby calculate the progress. I've written a command, named pmonitor, that displays the progress of processing a specified process or file. With it you can do things, such as the following.

    $ pmonitor -c gzip
    /home/dds/data/mysql-2015-04-01.sql.gz 58.06%
    

    An earlier version of Linux and FreeBSD shell scripts appears on my blog.

提交回复
热议问题