I'm running a shell script that pipes data from one process to another
process_a | process_b
Does anyone know a way to find out how many bytes were passed between the two programs? The only solution I can think of at the moment would be to write a small c program that reads from stdin, writes to stdout and counts all the of the data transfered, storing the count in an environment variable, like:
process_a | count_bytes | process_b
Does anyone have a neater solution?
Use pv the pipe viewer. It's a great tool. Once you know about it you'll never know how you lived without it.
It can also show you a progress bar, and the 'speed' of transfering.
Pipe through dd. dd's default input is stdin and default output is stdout; when it finishes stdin/stdout I/O, it will report to stderr on how much data it transferred.
If you want to capture the output of dd and the other programs already talk to stderr, then use another file-descriptor. Eg,
process_a | tee >(process_b) | wc --bytes
might work. You can then redirectwc
's count to where-ever you need it. Ifprocess_b
outputs anything tostdout
/stderr
you will probably need to redirect this off somewhere, if only/dev/null
.For a slightly contrived example:
By way of explanation:
tee
lets you direct output to multiple files (plus stdout) and the>()
construct is bash's "process substitution" which makes a process look like a write-only file in this case so you can redirect to processes as well as files (see here, or this question+answer for an example of usingtee
to send output to many processes).I know I'm late to the party, but I believe I have a good answer which can enhance this useful thread.
This is a mix of @Phil P and @David Spillett answer, but:
Bytes-count is printed to stdout, along with any output of process_b.
You can use a prefix to identify the line containing bytes when working with the output(
Bytes:
in the example).WARNING:
Do not rely on the order of the lines in the output
The order is unpredictable and it can always differ, even when calling the same script with the same parameters!