Bash howto write/read to/from 第一次发送后没有中止的命名管道
Bash howto write/read to/from a named pipe without aborting after first sending
给定:
Bash命令行(1号航站楼):
> mkfifo pipo
> cat pipo
Bash命令行(2号航站楼):
> echo -e "Hello World\nHi" > pipo
结果:
(1 号航站楼)中的 bash 打印:
Hello World
Hi
并中止。
问题:
我怎样才能实现它不会中止,但允许通过 pipo
发送另一个回显?
这是因为echo ... > fifo
打开然后关闭fifo。作为解决方法,您可以这样做:
# open for writing
exec 20> fifo
echo foo >&20
echo bar >&20
...
# to close it
exec 20>&-
一点解释:
exec 20> fifo
opens fifo
for writing with FD (file descriptor) 20.
command >&20
将输出重定向到 FD 20。
exec 20>&-
关闭 FD 20.
以下摘自man bash
:
exec [-cl] [-a name] [command [arguments]]
[...] If command is not specified, any redirections take effect in the current shell, and the return status is 0. If there is a redirection error, the return status is 1.
[n]>word
Redirection of output causes the file whose name results from the expansion of word to be opened for writing on file descriptor n, or the standard output (file descriptor 1) if n is not specified. If the file does not exist it is created; if it does exist it is truncated to zero size. [...]
[n]>&word
[...] If word evaluates to -
, file descriptor n is closed. [...]
在 1 号航站楼做
tail -f pipo
而不是 cat pipo
。
给定:
Bash命令行(1号航站楼):
> mkfifo pipo
> cat pipo
Bash命令行(2号航站楼):
> echo -e "Hello World\nHi" > pipo
结果:
(1 号航站楼)中的 bash 打印:
Hello World
Hi
并中止。
问题:
我怎样才能实现它不会中止,但允许通过 pipo
发送另一个回显?
这是因为echo ... > fifo
打开然后关闭fifo。作为解决方法,您可以这样做:
# open for writing
exec 20> fifo
echo foo >&20
echo bar >&20
...
# to close it
exec 20>&-
一点解释:
exec 20> fifo
opensfifo
for writing with FD (file descriptor) 20.command >&20
将输出重定向到 FD 20。exec 20>&-
关闭 FD 20.
以下摘自man bash
:
exec [-cl] [-a name] [command [arguments]]
[...] If command is not specified, any redirections take effect in the current shell, and the return status is 0. If there is a redirection error, the return status is 1.
[n]>word
Redirection of output causes the file whose name results from the expansion of word to be opened for writing on file descriptor n, or the standard output (file descriptor 1) if n is not specified. If the file does not exist it is created; if it does exist it is truncated to zero size. [...]
[n]>&word
[...] If word evaluates to
-
, file descriptor n is closed. [...]
在 1 号航站楼做
tail -f pipo
而不是 cat pipo
。