[Top][All Lists]

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

Re: output redirection with process substitution asynchronous?

From: pjodrr
Subject: Re: output redirection with process substitution asynchronous?
Date: Mon, 7 Dec 2009 23:17:18 -0800 (PST)
User-agent: G2/1.0

Hi Marc,

On Dec 7, 5:25 pm, Marc Herbert <address@hidden> wrote:
> Marc Herbert wrote:
> > What is wrong with the following:
> > prefix_with_date ()
> > {
> >     while read; do
> >         printf '%s: %s\n' "$(date)" "$REPLY";
> >     done
> > }
> > seq 4 | prefix_with_date
> > ls | prefix_with_date
> Sorry I missed the fact that you want to run your commands in the current 
> shell.
> There are no real coroutines in shell. The current shell process does
> not know how to schedule two pieces of code. So each time two
> pieces of code communicate through a pipe they have to be run in two
> *concurrent* processes (typically: subshells).  The producer and
> consumer of a pipe must run independently of each other. Whether you
> are using process substitution or the more usual and portable pipe "|"
> does not matter here: you need concurrency.
> So at least one of: 1. your prefixer code, 2. your unknown command
> has to run in a independent process, "asynchronous" with the current
> shell.
> Since you absolutely want your unknown commands to run in the current
> shell, then it is your "prefixing" code that has to run in a
> concurrent process.
> Now I do not really see any other way to avoid the ugliness of
> concurrently printing on stdout than to "wait" for your concurrent
> prefixer to complete, more or less like you did.
> A variant is to ask socat to handle the cleanup actions for you like
> this:
> prefix_with_date()
> {
>     local P=/tmp/dataorig.$$
>     socat -u  PIPE:${P}  SYSTEM:'while read; do echo "$(date):\\ $REPLY"; 
> done'   &
>     socatPID=$!
>     until [ -e ${P} ]; do sleep 1; done
>     $@ > ${P}
>     wait $socatPID
> }
> prefix_with_date  seq 5

thank you very much for your explanation!  I feel understood  and will
think about
using socat and if I want my script to depend on another program.  But
at least
it became clear that the builtin process substitution is not the
solution for me.




reply via email to

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