Question

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.



EDIT (Moved from below -- OP submitted this clarification as an answer)

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.

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.

Was it helpful?

Solution

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.

OTHER TIPS

Maybe you could tweak 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 could be combined with netcat (or nc) or ncat (which is part of nmap) for communicating between different ttys?

For more information see:

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

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:

$ tty
/dev/pts/2

In sending xterm:

$ echo howdy doody > /dev/pts/2

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

$ cat > /dev/pts/2

You have to chmod the permissions to write to /dev/pts/2 if your doing this across users.

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).

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.

it is simple you just need 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/pts/4 or exec < cat /dev/pts/4

and type something in 4 th one you now know what happens

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top