[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] How to not terminate a process using pipe?
From: |
Pierre Gaston |
Subject: |
Re: [Help-bash] How to not terminate a process using pipe? |
Date: |
Thu, 22 Feb 2018 10:09:20 +0200 |
On Thu, Feb 22, 2018 at 9:00 AM, Pierre Gaston <address@hidden>
wrote:
>
>
> On Thu, Feb 22, 2018 at 1:07 AM, Peng Yu <address@hidden> wrote:
>
>> > The echo will open and close the the file refered to by in and out.
>> > The close action will cause cat to quit.
>> >
>> > What you need to do is to keep the files open, like
>> >
>> > mkfifo in out
>> > exec 111<in 222>out
>> >
>> > echo x >&111
>> > read -r x <&222
>> >
>> > echo x >&111
>> > read -r x <&222
>> >
>> > # Close the file descriptors.
>> > exec 111<&- 222<&-
>>
>> This does not seem to work as it hangs there.
>>
>> /tmp$ cat main1.sh
>> #!/usr/bin/env bash
>> # vim: set noexpandtab tabstop=2:
>>
>> tmpdir=$(mktemp -d)
>> mkfifo "$tmpdir"/infifo "$tmpdir"/outfifo
>> set -v
>> exec 111<"$tmpdir"/infifo 222>"$tmpdir"/outfifo
>>
>> echo x >&111
>> read -r x <&222
>>
>> echo x >&111
>> read -r x <&222
>>
>> # Close the file descriptors.
>> exec 111<&- 222<&-
>>
>> /tmp$ ./main1.sh
>> exec 111<"$tmpdir"/infifo 222>"$tmpdir"/outfifo
>>
>>
>> --
>> Regards,
>> Peng
>>
>>
> This script by itself does nothing, the pipe is blocking so you need at
> least another process that reads and writes to these fifos.
> For instance, this "works" with the kind of cat you had in your first
> request (I added some echo to print something):
>
Sorry I messed up by copy/pasting bits and pieces in my previous example is
incorrect (wrong redirection directions for the cat process).
Hopefully this one is working:
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:
rm -f in out
mkfifo in out
cat < in >out &
exec 111>in 222<out
echo foo >&111
read -r x <&222
echo x: $x
echo bar >&111
read -r x <&222
echo x: $x
exec 111<&- 222<&-