Ubuntu: How to use pv command for executable file?



Question:

I know that I can use pv command for showing progress of the command. for example:

for i in {1..200}; do sleep 0.1; echo "$i"; done | pv -ls 200 > output.txt  

But when I have a file main.c which have the following code:

FILE * file;  file = fopen("output.txt", "a");  for(int i=0; i<200; i++){      fprint(file, "%d\n", i+1);  }  fclose(file);  

and when I execute this file, I have a.out executable file. So, now I want to use pv command for showing the progress of ./a.out command. Is there is any way to do that??


Solution:1

As I've mentioned in my comment, the core issue is that your a.out writes to a file, not to stdin stream, which means pv can receive no data. When your program outputs data to stdin stream, then it works:

$ ./a.out | pv -ls 200 > /dev/null                                    201  0:00:00 [2.09M/s] [========================>] 100%                $ cat main.c  #include<stdio.h>    int main(){      int i=0;      for(i;i<=200;i++)          fprintf(stdout,"%d\n",i);      return 0;  }  

By the way, you don't have to use fprintf() to write to stdout, a simple printf("%d\n",i); can be sufficient


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »