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 ?

jfs
  • 399,953
  • 195
  • 994
  • 1,670
shanky061
  • 21
  • 1
  • 6
  • Do you have some need to do it exactly this way? It seems a fifo could work. Here's an example of [mkfifo](http://www.linuxjournal.com/article/2156). – Micah Elliott Jul 15 '15 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 Jul 15 '15 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//fd/0` in another terminal. – jfs Jul 15 '15 at 22:18
  • yes it works that way but if we `echo bar > /proc//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 Jul 15 '15 at 23:00

2 Answers2

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

Greg Hilston
  • 2,397
  • 2
  • 24
  • 35
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.

o11c
  • 15,265
  • 4
  • 50
  • 75
  • also `/dev/pts/2` is a file so when we write something to it , a program reading from it should read that. – shanky061 Jul 15 '15 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 Jul 15 '15 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 Jul 16 '15 at 00:03
  • and if cat is a _slave_ then how does _slave_ get data from _master_ ? – shanky061 Jul 16 '15 at 00: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 Jul 16 '15 at 00:36
  • @shanky061 Anything you're doing is a slave only. – o11c Jul 16 '15 at 00:48