1

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 ?

4
  • Do you have some need to do it exactly this way? It seems a fifo could work. Here's an example of mkfifo. Commented Jul 15, 2015 at 21:11
  • @MicahElliott i know that using fifo it will work but just want to know why directly sending to its stdin doesn't work.
    – shanky061
    Commented Jul 15, 2015 at 22:02
  • It works if input is a pipe e.g., run cat | tr a-z A-Z in one terminal; then echo aBc > /proc/<pid of tr>/fd/0 in another terminal.
    – jfs
    Commented Jul 15, 2015 at 22:18
  • yes it works that way but if we 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.
    – shanky061
    Commented Jul 15, 2015 at 23:00

2 Answers 2

1

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

3
  • cat doesn't repeating the data received from another tty using your method, also don't give me fifo solutions.
    – shanky061
    Commented Jul 15, 2015 at 22:12
  • 1
    Or run tty to see your own TTY name.
    – o11c
    Commented Jul 15, 2015 at 22:16
  • Sorry it didn't help. Wish you luck! @o11c I did not know that. Great to know! Commented Jul 16, 2015 at 13:03
0

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.

6
  • also /dev/pts/2 is a file so when we write something to it , a program reading from it should read that.
    – shanky061
    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.
    – o11c
    Commented 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.
    – shanky061
    Commented Jul 16, 2015 at 0:03
  • and if cat is a slave then how does slave get data from master ?
    – shanky061
    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..
    – shanky061
    Commented Jul 16, 2015 at 0:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.