使用 Bash 将标准输入重定向到来自不同的终端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/640493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
redirect the stdin to come from a different terminal using Bash
提问by scorpio
I'm wondering how one would go about redirecting the stdin of a script from the current xterm session i.e. /dev/pts/0 to one that is also running i.e /dev/pts/1 using bash? I have a bash script that opens 3 xterm windows and I want to get input from only one of those windows and I cannot figure out how to do it. Any help is appreciated! thanks.
我想知道如何将脚本的标准输入从当前的 xterm 会话(即 /dev/pts/0)重定向到一个也在使用 bash 运行的脚本,即 /dev/pts/1?我有一个 bash 脚本,可以打开 3 个 xterm 窗口,我只想从其中一个窗口获取输入,但我不知道该怎么做。任何帮助表示赞赏!谢谢。
EDIT编辑(从下面移动 - OP 提交了此澄清作为答案)
I guess I should have clarified what I wanted to do. I will start a script from a pty, let's say it's /dev/pts/3. This script will open 3 xterminals, lets say: /dev/pts/0, /dev/pts/1, and /dev/pts/2. These 3 new ptys are what the user is going to see. The script asks the user for some input and I want the input of the user to be typed into /dev/pty/1 and the program should get it's info from there. However I've tried to do this and it doesn't work. Here's a snippet of my code.
我想我应该澄清我想做什么。我将从 pty 开始一个脚本,假设它是 /dev/pts/3。该脚本将打开 3 个 xterminal,比如:/dev/pts/0、/dev/pts/1 和 /dev/pts/2。这 3 个新 pty 是用户将要看到的。该脚本要求用户提供一些输入,我希望将用户的输入输入到 /dev/pty/1 中,程序应该从那里获取它的信息。但是我试过这样做,但它不起作用。这是我的代码片段。
exec</dev/pts/1
echo
echo "Would you like to search for more info?" 1>/dev/pts/1
read answer
case $answer in
y) echo "YES" ;;
n) echo "NO" ;;
*) echo "y/n only!";;
esac
The case statement at the end is just a little placeholder to see if the input actually worked.
最后的 case 语句只是一个小占位符,用于查看输入是否确实有效。
回答by soeren
Maybe you could tweak ttyechofor your needs?
也许您可以ttyecho根据自己的需要进行调整?
# /dev/ttysXXX is the result of the tty command in another Terminal window
sudo ttyecho -n /dev/ttysXXX pwd
And maybe ttyechocould be combined with netcat(or nc) or ncat(which is part of nmap) for communicating between different ttys?
也许ttyecho可以与netcat(or nc) 或ncat(nmap 的一部分)结合使用,以便在不同的 tty 之间进行通信?
For more information see:
有关更多信息,请参阅:
- Utility to Send Commands or Data to Other Terminals (tty/pts)-- (ttyecho)
- create read/write environment using named pipes
- 向其他终端发送命令或数据的实用程序 (tty/pts)-- (ttyecho)
- 使用命名管道创建读/写环境
回答by rmeador
I suspect this is not possible. AFAIK, without modifying something in kernel space, it's impossible to read the input from a tty (or pty) that is not the current tty. Even root can't do it. I spent some time looking into this and I was unable to find out how to do it, but I did find lots of sources claiming it was impossible. This appears to have been a design decision to increase security/privacy of users.
我怀疑这是不可能的。AFAIK,如果不修改内核空间中的某些内容,就不可能从不是当前 tty 的 tty(或 pty)读取输入。连root都做不到。我花了一些时间研究这个,但我无法找到如何去做,但我确实找到了很多声称这是不可能的消息来源。这似乎是提高用户安全性/隐私性的设计决策。
回答by Ian Kelling
To answer your clarified question, a simple way is to use a FIFO (named pipe) for the job. On sending terminal:
要回答您澄清的问题,一种简单的方法是为作业使用 FIFO(命名管道)。在发送终端上:
mkfifo ./myfifo
read var
echo "var" > myfifo
On the recieving terminal:
在接收终端上:
read line < ./myfifo
To simply print an another xterm from your own, in recieving xterm:
要简单地从您自己的 xterm 中打印另一个 xterm,在接收 xterm 时:
$ tty
/dev/pts/2
In sending xterm:
在发送 xterm 时:
$ echo howdy doody > /dev/pts/2
Or from a script in the sending xterm, redirecting stdin as you asked:
或者从发送 xterm 中的脚本,按照您的要求重定向 stdin:
$ cat > /dev/pts/2
You have to chmod the permissions to write to /dev/pts/2 if your doing this across users.
如果跨用户执行此操作,则必须修改写入 /dev/pts/2 的权限。
You cannot capture whats printed this way on the recieving terminal. There is no builtin redirection method to capture the input from another terminal.
您无法在接收终端上捕获以这种方式打印的内容。没有内置重定向方法来捕获来自另一个终端的输入。
If you want an automated way for the sending xterm to find out the character device of the receiving one, that could be answered several ways depending on what kind of interprocess communication you want to use. A simple hack would be for the receiver to do tty > file1 and the sender to do echo whatever > $(cat file1).
如果您想要一种自动方式让发送 xterm 找出接收者的字符设备,可以根据您要使用的进程间通信类型来回答几种方式。一个简单的技巧是让接收者执行 tty > file1,而发送者执行 echo 任何 > $(cat file1)。
If you want to try and direct this from the receiver instead of the sender, again you have an interprocess communication problem that can be solved in several ways.
如果您想尝试从接收者而不是发送者直接进行此操作,那么您又会遇到一个可以通过多种方式解决的进程间通信问题。
回答by tCheang
it is simple you just need understand
很简单,你只需要了解
ls -ls /dev/pts you will see
ls -ls /dev/pts 你会看到
0 1 2 3 4 suppose you open several
0 1 2 3 4 假设您打开多个
now use one NOT 4 and type cat < /dev/pts/4 or exec < cat /dev/pts/4
现在使用一个 NOT 4 并输入 cat < /dev/pts/4 或 exec < cat /dev/pts/4
and type something in 4 th one you now know what happens
并在第 4 个中输入一些内容,您现在知道会发生什么

