Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect the stdin to come from a different terminal using Bash

Tags:

linux

bash

stdin

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.

like image 379
scorpio Avatar asked Dec 01 '22 08:12

scorpio


2 Answers

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:

  • Utility to Send Commands or Data to Other Terminals (tty/pts) -- (ttyecho)
  • create read/write environment using named pipes
like image 94
soeren Avatar answered Dec 04 '22 04:12

soeren


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.

like image 40
rmeador Avatar answered Dec 04 '22 03:12

rmeador