bug-bash
[Top][All Lists]
Advanced

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

Re: output redirection with process substitution asynchronous?


From: Greg Wooledge
Subject: Re: output redirection with process substitution asynchronous?
Date: Mon, 7 Dec 2009 08:41:49 -0500
User-agent: Mutt/1.4.2.3i

> pjodrr wrote:
> > how can I prefix every line of output of some command with a
> > timestamp? 

Mark 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


On Sun, Dec 06, 2009 at 12:49:44AM -0800, pjodrr wrote:
> fifo=$(mktemp -u) || exit
> mkfifo $fifo || exit
> trap "rm -f $fifo" 0
> trap exit 1 2 15
> while read line; do echo "$(date): $line"; done < $fifo &
> prefix_pid=$!
> seq 4 > $fifo
> wait $prefix_pid
> 
> Or how would you accomplish this?

What on earth is "this"?  If you just want to prefix every line of some
command's output with a timestamp, and you're not willing to use
multilog (http://cr.yp.to/daemontools.html) or a perl one-liner, then
the bash function Mark gave is quite reasonable (maybe use "read -r"
instead of "read" to preserve backslashes).

The obvious disadvantage of doing this in bash is that bash has to
invoke an external date(1) command for every line it processes.
A perl one-liner could do it with built-in time functions, and of
course multilog is a single program.

It seems you have some other goal in mind, though, besides "prefix every
line of output of some command with a timestamp".  What is the point of
all this command substitution and FIFO trickery?




reply via email to

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