When i run cat -
in say /dev/pts/2
and try to write to its input from another tty with echo foo > /dev/pts/2
or echo foo > /proc/(pid of cat)/fd/0
it just prints foo in pts/2, cat doesn't repeat it . why?
How to send input to cat from another tty so it also repeat it ?
2 Answers
Each and every terminal has a file for it, in /dev/pts/.
$ ps
to determine which terminal you are on. Example: I am on terminal 3
PID TTY TIME CMD
1477 pts/3 00:00:00 ps
26511 pts/3 00:00:01 bash
Than just redirect your output to that terminal.
cat foo > /dev/pts/3
Make a first in first out pipe on the second terminal, the one you'd like to display the text on
mkfifo --mode=600 /tmp/pipe
Redirect the command to that pipe on the first terminal
cat foo > /tmp/pipe
-
cat doesn't repeating the data received from another tty using your method, also don't give me fifo solutions. Commented Jul 15, 2015 at 22:12
-
1
-
Sorry it didn't help. Wish you luck! @o11c I did not know that. Great to know! Commented Jul 16, 2015 at 13:03
I think there is a fundamental misunderstanding here: you cannot inject content to another TTY's input stream (unless you own the master).
You can, however, call cat /dev/pts/0
to read from another TTY's input stream, but beware that you will be fighting whatever process is already there.
-
also
/dev/pts/2
is a file so when we write something to it , a program reading from it should read that. Commented Jul 15, 2015 at 23:33 -
@shanky061 Sure, a program reading from the master end will read it. Just like when the master writes, the slave can read.– o11cCommented Jul 15, 2015 at 23:35
-
and who is the master ?, i think cat is reading from it ?? no, then how cat reads from it , cat's fd/0 file is linked to it. Commented Jul 16, 2015 at 0:03
-
and if cat is a slave then how does slave get data from master ? Commented Jul 16, 2015 at 0:13
-
it said : Data written to master is presented to slave as input and for a corresponding slave file there should be master file but i have 3 slave file and 1 master file in pts directory and when i try to write to master file with root permissions it does nothing.. Commented Jul 16, 2015 at 0:36
cat | tr a-z A-Z
in one terminal; thenecho aBc > /proc/<pid of tr>/fd/0
in another terminal.echo bar > /proc/<pid of cat>/fd/0
then after reading from its file descriptor 0 cat should write same to its file descriptor 1 which should be passed to tr though pipe and tr should write BAR to terminal. But that's not happening.