tags:

views:

1947

answers:

3

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

A: 

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.

rmeador
I don't think this deserves a downvote, especially not without a comment telling me how I'm wrong. Now, in light of Ian Kelling's answer, I may have misunderstood the OP's intent, in which case my answer may not apply, but it is still true AFAIK. Please see my comment on that answer.
rmeador
+2  A: 

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.

Ian Kelling
I've tried doing this in the past and what you end up with is "howdy doody" printed in the /dev/pts/2 window. It does not get sent as if the user typed it (stdin), it gets sent as if the shell printed it (stdout). Did I completely misunderstand the OP's question?
rmeador
A: 

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.

scorpio
on StackOverflow, the general rule is that you should edit your question instead of replying to it with more info (after all, this isn't an answer). Btw, I still think this is not possible, but I'd be happy to be proved wrong.
rmeador
see my updated answer.
Ian Kelling