bashwhile-looppipeprogress-barviewer

Pipe-view a loop sequence (add a progress bar to While or For loop) - Bash


As posted here and here, pipeviewer is a tool that shows content progress based on it's size. As seen there, the proposal of their questions is to get a progress bar of a process running without data volume.

I was wondering if is it possible to show progress of a loop with pipeviewer, considering that I'm reading it from a file, and I know it's size.

I've trying something like

while IFS= read -r line;
 do
   <code>
 done < file.txt | pv

And this definitelly doesn't work, since pv shows only an empty progress bar.

Any ideas?

Thank you in advance!


Solution

  • If you can, read the file with pv instead of cat, so that pv will automatically get the file size and format the progress bar appropriately.

    For example:

    pv very_big_file.txt
    

    or, in your example:

    pv file.txt | while IFS= read -r line;
    do
        <code>
    done
    

    If you cannot read the file with pv, you can pass pv the size of the file with -s size. That way, pv will expect the flow to be that length, and format the progress bar proportionally to it.

    You can get the size of a file with:

    stat -c '%s' file
    

    or

    wc -c < file
    

    For example:

    command1 | command2 | ... | pv -s $(stat -c '%s' file) | commandX | ...
    

    in your example:

    cat file.txt | pv -s $(stat -c '%s' file.txt) | while IFS= read -r line;
    do
        <code>
    done
    

    As you see, it is redundant to use pv just after cat, it should be substituted by a pv reading the file.