[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: mkfifo and tee within a function
From: |
Chet Ramey |
Subject: |
Re: mkfifo and tee within a function |
Date: |
Tue, 05 Dec 2006 23:23:02 -0500 |
User-agent: |
Thunderbird 1.5.0.8 (Macintosh/20061025) |
Nathan Coulter wrote:
> Hi,
>
> Within a function, I'd like to print some input to the terminal and at
> the same time store it in a fifo fostore some input in a fifo, but am
> getting mixed results. In this example, I thought "hello" would be
> output twice:
>
> $>cmd_print () { mkfifo zout ; (tee zout &); cat zout ; rm zout; }
> $>printf 'hello\n' | cmd_print
When job control is not active, Posix requires that a command run in the
background with `&' behave as if its standard input were /dev/null in the
absence of any explicit redirection. Bash counts a pipe directly into
such a command as one such redirection, but not the pipe into the shell
function. This function works:
cmd_print1 () { mkfifo zout1 ; (tee zout1 </dev/stdin &) ; cat zout1 ; rm
zout1; }
It adds an explicit redirection from stdin.
>
> This outputs "hello" twice:
>
> $>cmd_print () { mkfifo zout ; (cat - | tee zout &); cat zout ; rm zout; }
> $>printf 'hello\n' | cmd_print
> hello
> hello
This behaves as you expect because of the pipe into the asynchronous `tee'
command.
>
> but this doesn't output anything:
>
> $>cmd_print () { mkfifo zout ; (cat - | tee zout &) ; rm zout; }
> $>cmd_print <<EOF
> $> date
> $> EOF
I believe this is caused by a race condition: the rm runs before the
pipeline, and the `tee' is either orphaned or writes to a regular file
named `zout'. I'm not sure how to make this form work reliably.
>
> My real goal is to feed data to a function and then source it:
> $> cmd_print () { mkfifo zout ; (cat - > zout ) & ; source zout ; rm
> zout; }
> $> cmd_print <<EOF
> $> date
> $> EOF
> $>
There is a limitation in current releases of bash, which will be fixed
in the next release, that restricts `source' to work only on regular
files. Once that is fixed, the same sort of explicit redirection of
input from /dev/stdin as in the first example will work:
cmd_print4 () { mkfifo zout4 ; (cat - > zout4) </dev/stdin & source zout4 ;
rm zout4; }
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
Live Strong. No day but today.
Chet Ramey, ITS, CWRU chet@case.edu http://cnswww.cns.cwru.edu/~chet/
- Re: mkfifo and tee within a function,
Chet Ramey <=