Skip to main content
added 226 characters in body
Source Link
Jirka
  • 121
  • 3

Starting from 8.24 version of coreutils there is another way:

tee --output-error=warnp </dev/zero >(head -c100Mc10M | wc -c ) > >(head -c1 | wc -c ) >/dev/full | numfmt --to=iec

The output is

1

10M

The crucial points here are

  1. Use --output-error=warn which will cause tee to issue only warning if fwrite returns EPIPE error instead of finishing. tee will run as long as there is at least one output file opened for writing.

    Use -p which will cause tee to warn on errors writing to any output not a pipe. This means that EPIPE error on fwrite will not cause tee to exit and tee will run as long as there is at least one output file opened for writing. If you would like to get warning about EPIPE printed out to stderr use --output-error=warn instead -p.

  2. Use >/dev/full to close tee's stdout. This is important for two reasons. If you use >/dev/null for example tee would run as long as stdin has data (forever in this case). At the same time it will allow output of >(process substitutions) to be send to stdout for the further processing.

    Use > >(head -c1 | wc -c ) to redirect tee's stdout to another >(process substitution). This is important for two reasons. You need to take care of stdout output produced by tee. If you use >/dev/null for example tee would run as long as stdin has data (forever in this case). At the same time it will allow output of >(process substitution) to be send to stdout for the further processing. Other possibilities but not so elegant are to close tee's stdout output by other means. Two possibilities are >/dev/full and >&-. These will however issue a warnings (tee: standard output: No space left on device and tee: standard output: Bad file descriptor respectively)

Personally, I would love to have --no-stdout option implemented in tee rather than having to use >/dev/null trick. See this e-mail thread http://lists.gnu.org/archive/html/coreutils/2015-11/msg00070.html and post your arguents there if you would like to have --no-stdout option implemented in tee.

Starting from 8.24 version of coreutils there is another way:

tee --output-error=warn </dev/zero >(head -c100M | wc -c ) >(head -c1 | wc -c ) >/dev/full | numfmt --to=iec

The crucial points here are

  1. Use --output-error=warn which will cause tee to issue only warning if fwrite returns EPIPE error instead of finishing. tee will run as long as there is at least one output file opened for writing.
  2. Use >/dev/full to close tee's stdout. This is important for two reasons. If you use >/dev/null for example tee would run as long as stdin has data (forever in this case). At the same time it will allow output of >(process substitutions) to be send to stdout for the further processing.

Personally, I would love to have --no-stdout option implemented in tee rather than having to use >/dev/null trick. See this e-mail thread http://lists.gnu.org/archive/html/coreutils/2015-11/msg00070.html and post your arguents there if you would like to have --no-stdout option implemented in tee.

Starting from 8.24 version of coreutils there is another way:

tee -p </dev/zero >(head -c10M | wc -c ) > >(head -c1 | wc -c ) | numfmt --to=iec

The output is

1

10M

The crucial points here are

  1. Use -p which will cause tee to warn on errors writing to any output not a pipe. This means that EPIPE error on fwrite will not cause tee to exit and tee will run as long as there is at least one output file opened for writing. If you would like to get warning about EPIPE printed out to stderr use --output-error=warn instead -p.

  2. Use > >(head -c1 | wc -c ) to redirect tee's stdout to another >(process substitution). This is important for two reasons. You need to take care of stdout output produced by tee. If you use >/dev/null for example tee would run as long as stdin has data (forever in this case). At the same time it will allow output of >(process substitution) to be send to stdout for the further processing. Other possibilities but not so elegant are to close tee's stdout output by other means. Two possibilities are >/dev/full and >&-. These will however issue a warnings (tee: standard output: No space left on device and tee: standard output: Bad file descriptor respectively)

Source Link
Jirka
  • 121
  • 3

Starting from 8.24 version of coreutils there is another way:

tee --output-error=warn </dev/zero >(head -c100M | wc -c ) >(head -c1 | wc -c ) >/dev/full | numfmt --to=iec

The crucial points here are

  1. Use --output-error=warn which will cause tee to issue only warning if fwrite returns EPIPE error instead of finishing. tee will run as long as there is at least one output file opened for writing.
  2. Use >/dev/full to close tee's stdout. This is important for two reasons. If you use >/dev/null for example tee would run as long as stdin has data (forever in this case). At the same time it will allow output of >(process substitutions) to be send to stdout for the further processing.

Personally, I would love to have --no-stdout option implemented in tee rather than having to use >/dev/null trick. See this e-mail thread http://lists.gnu.org/archive/html/coreutils/2015-11/msg00070.html and post your arguents there if you would like to have --no-stdout option implemented in tee.