How to add a progress bar to a shell script?

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

    This lets you visualize that a command is still executing:

    while :;do echo -n .;sleep 1;done &
    trap "kill $!" EXIT  #Die with parent if we die prematurely
    tar zxf packages.tar.gz; # or any other command here
    kill $! && trap " " EXIT #Kill the loop and unset the trap or else the pid might get reassigned and we might end up killing a completely different process
    

    This will create an infinite while loop that executes in the background and echoes a "." every second. This will display . in the shell. Run the tar command or any a command you want. When that command finishes executing then kill the last job running in the background - which is the infinite while loop.

提交回复
热议问题