我有一个 bash 脚本,我想在标准输出中与用户通信,但也通过文件描述符将命令发送到子进程 - 如下所示:
# ...
# ...
echo "Hello user, behold a cleared gnuplot window"
# pass the string "clear" to gnuplot via file descriptor 3
echo "clear" >&3
所以我想我可以通过首先启动这样的子流程来“设置”:
#!/bin/bash
# Initiate(?) file descriptor 3, and let it direct to a newly
# started gnuplot process:
exec >3 >( gnuplot )
但这会产生一个错误:
/dev/fd/63: Permission denied
这是可以预料的吗?
我不明白发生了什么事。(我做错了什么吗?可能是我的系统有一些特殊的安全设置,不允许我尝试做的事情吗?(运行 Ubuntu Linux 12.10。))
“解决方法” - 以下似乎与我正在尝试做的相同,并且可以正常工作:
#!/bin/bash
# open fd 3 and direct to where fd 1 directs to, i.e. std-out
exec 3>&1
# let fd 1 direct to a newly opened gnuplot process
exec 1> >( gnuplot )
# fd 1 now directs to the gnuplot process, and fd 3 directs to std-out.
# I would like it the other way around. So we'll just swap fd 1 and 3
# (using an extra file descriptor, fd 4, as an intermediary)
exec 4>&1 # let fd 4 direct to wherever fd 1 directs to (the gnuplot process)
exec 1>&3 # let fd 1 direct to std-out
exec 3>&4 # let fd 3 direct to the gnuplot process
exec 4>&- # close fd 4
或者,作为单行:
#!/bin/bash
exec 3>&1 1> >( gnuplot ) 4>&1 1>&3 3>&4 4>&-
为什么这行得通,但初始版本却不行?
非常感谢任何帮助。
$ bash --version
GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu)
[...]