help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: tee >(sponge f1) >(sponge f2) to pipeline form


From: Koichi Murase
Subject: Re: tee >(sponge f1) >(sponge f2) to pipeline form
Date: Fri, 10 Apr 2020 06:49:26 +0900

2020-04-10 4:26 Peng Yu <address@hidden>:
> Does anybody know whether there is a way to rewrite it in a pipeline
> form so that errors of sponge will be captured? Thanks.

You can use exactly the same approach with `<()' case.

set -o pipefail
{
  {
    {
      tee /dev/fd/$fd1 /dev/fd/$fd2 >&$stdout
    } {fd1}>&1 | sponge f1
  } {fd2}>&1 | sponge f2
} {stdout}>&1

----------

You can extend a function (pipesubst) in
  https://lists.gnu.org/archive/html/help-bash/2020-04/msg00007.html
so that it supports both `>()' and '<()' types of substitutions something like:

## usage: pipesubst COMMAND SUBST
##   SUBST has the form of "> command" or "< command"
function pipesubst {
  local __command; pipesubst__QuoteCommand __command "$1"; shift
  __command+=' >&$__fd1'

  local __subst __qsubst __index=1
  for __subst; do
    __command="{ local pipe$__index=/dev/fd/\$fd$__index; $__command; }"
    pipesubst__QuoteCommand __qsubst "${__subst#[<>]}"
    if [[ $__subst == \>* ]]; then
      __command="local fd$__index; $__command {fd$__index}>&1 | $__qsubst"
    else
      __command="local fd$__index; $__qsubst | $__command {fd$__index}>&1"
    fi
    ((__index++))
  done
  local __fd1; eval -- "$__command" {__fd1}>&1
}
function pipesubst__QuoteCommand {
  local q=\' Q="'\''"
  printf -v "$1" "eval -- '${2//$q/$Q}'"
}

Then,

set -o pipefail
pipesubst 'tee "$pipe1" "$pipe2"' \
          '> sponge f1' \
          '> sponge f2'

--
Koichi



reply via email to

[Prev in Thread] Current Thread [Next in Thread]