1

In my fish shell, I am using fish command timer to automatically show the duration of each command after it has been executed. This is a neat feature, which I recommend.

Now I want something similar in addition to that: The number of lines, i.e. after each command line I execute, the number of lines printed to stdout and/or stderr should be printed to stdout, too.

Example:

Instead of

> cat /etc/mpd.conf
pid_file "/run/mpd/mpd.pid"
db_file "/var/lib/mpd/mpd.db"
state_file "/var/lib/mpd/mpdstate"
playlist_directory "/var/lib/mpd/playlists"

I want to see

> cat /etc/mpd.conf
pid_file "/run/mpd/mpd.pid"
db_file "/var/lib/mpd/mpd.db"
state_file "/var/lib/mpd/mpdstate"
playlist_directory "/var/lib/mpd/playlists"

[ 4 lines ]

Some notes:

  • There are some manual solutions to print the number of output lines of a command here, but I want this to be a permanent feature implemented in my fish config.
  • I don't want to execute the command twice (for obvious reasons like the lack of idempotency etc.)
  • A solution for bash (instead of fish) may point in the right direction.

The command timer works with a postexec event that is fired after executing a command line, but that does not exactly solve my problem, so I am in need of an approach.

1

Try replacing cat /etc/mpd.conf with cat /etc/mpd.conf | tee /proc/self/fd/2 | echo -e "\n[ $(wc -l) lines ]"

If you can't use /proc/self/fd/ try splitting it into 2 commands or use the && operator for something like cat /etc/mpd.conf && echo "Number of lines: $(cat /etc/mpd.conf | wc -l)"

from wc man page:

-l, --lines print the newline counts

  • As I understand, the author of question wants solution, that will work for each command, not only the cat: " The number of lines, i.e. after each command line I execute, the number of lines printed to stdout and/or stderr should be printed to stdout, too." – MiniMax May 10 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.