5

我有一个 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)
[...]
4

2 回答 2

1

你有一个错字;使用exec 3> >( gnuplot )而不是exec >3 >( gnuplot ).

顺便说一句,是的,这是意料之中的。exec >3 >( gnuplot )将 stdout 重定向到名为 3 的文件,然后尝试作为>( gnuplot )程序执行(转换为 /dev/fd/63)。

于 2013-01-08T01:04:24.107 回答
1

我得到:

/dev/fd/63:权限被拒绝

由于使用 sudo 导致进程替换,如此所述。

所以要这样做:

$ sudo ruby <(echo "puts 'foo'")
ruby: Bad file descriptor -- /dev/fd/63 (LoadError)
于 2013-03-29T18:00:02.870 回答