Shell – How to display the number of lines output by a command in real time

pipeprogress-informationshell

I'm using svn export as part of a packager script for my application, and it looks like this command, like many others, doesn't have any kind of progress bar.

I have two choices at the moment:

  • using it without options, and watch it printing thousands of lines
  • using --quiet, and not seing anything until it completes.

Is there a way to at least show the number of lines output by the command, in real time? Such as:

Exporting SVN directory ... 1234 files

And see this number 1234 increment in real time? I can imagine piping the output to a command that would do just this, but which one?

Best Answer

yourcommand | { I=0; while read; do printf "$((++I))\r"; done; echo ""; }

Or put the bracketed section in a shell script. Note that this only works if your shell actually supports the preincrement operator, like bash or ksh93 or zsh. Otherwise you'll have to increment $I and then print it (as in I=$((I+1));printf...). Also, if printf isn't a builtin with your shell (it's a builtin with current bash), you could use echo -ne or print -n instead for better performance. You just want to suppress the newline and have the \r interpreted as an escape character.