Redirect stdin from another terminal using Bash

I am wondering how one could redirect stdin from a script from the current xterm session ie / dev / pts / 0 to one that also works ie / 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 these windows, and I cannot figure out how to do this. Any help is appreciated! thanks.



EDIT (moved from bottom - OP provided this clarification as an answer)

I think I should have clarified what I wanted to do. I ran the script from pty, say / dev / pts / 3. This script will open 3 xterminals, say: / dev / pts / 0, / dev / pts / 1 and / dev / pts / 2. These 3 new pty are what the user will see. The script asks the user for some input, and I want to enter the user input in / dev / pty / 1, and the program should get information from it. However, I tried to do this and it does not work. Here is a snippet of my code.

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 really works.

+4
source share
4 answers

I suspect this is not possible. AFAIK, without changing anything in kernel space, it is impossible to read input with tty (or pty), which is not the current tty. Even root cannot do this. I spent some time on this, and I could not find out how to do this, but I found many sources claiming that this was impossible. Apparently, it was a design solution to increase the security / privacy of users.

+5
source

Perhaps you can customize ttyecho for your needs?

 # /dev/ttysXXX is the result of the tty command in another Terminal window sudo ttyecho -n /dev/ttysXXX pwd 

And maybe ttyecho can be combined with netcat (or nc ) or ncat (which is part of nmap) to communicate between different ttys?

For more information see

+7
source

To answer your clarified question, a simple way is to use FIFO (named pipe) for the job. When sending the terminal:

 mkfifo ./myfifo read var echo "var" > myfifo 

On the receiving terminal:

 read line < ./myfifo 

To just print another xterm from your own, in getting xterm:

 $ tty /dev/pts/2 

When sending xterm:

 $ echo howdy doody > /dev/pts/2 

Or from a script in the sending xterm, redirecting stdin as you requested:

 $ cat > /dev/pts/2 

You have chmod write permissions in / dev / pts / 2 if you do this through users.

You cannot fix what is printed in this way on the receiving terminal. There is no built-in redirection method to capture input from another terminal.

If you need an automated way to send xterm to recognize the character device of the receiver, which can be answered in several ways, depending on what kind of communication between the processes you want to use. A simple hack would be for the receiver to do tty> file1 and the sender to do echo what> $ (cat file1).

If you want to try to forward this from the receiver instead of the sender, again you will have a communication problem between processes that can be solved in several ways.

+5
source

it's just you just need to understand

ls -ls / dev / pts you will see

0 1 2 3 4 suppose you open several

now use one NOT 4 and type cat </ dev / glasses / 4 or exec <cat / dev / pts / 4

and enter something in the 4th you now know what will happen

+1
source

All Articles