Read input from another tty

Hello,

I am trying to find out how I can find the tty number of a terminal a bash script has just created.

Ultimately what I am trying to achieve is a script that starts a new terminal where stderr can be directed to.

ls -l missing_file 2> /dev/pts/X

To find the tty number of the new terminal I have

#!/bin/bash
tty_current=`tty`
echo $tty_current
xterm -e "tty > $tty_current" &

This will output the tty number of both the "parent and child" terminals.

My problem is how can I read in the output from the new terminal?

Are there any other methods people know of to achieve the same thing?

Regards,
Colin

You haven't said which OS, etc., you are on, so you might have to adapt the following a bit:

In principle every device in Unix/Linux has a "device file", which resides in /dev . In the case of a terminal this is most probably /dev/ttyX , where "X" is some number and the device file is a character device. For instance, from my Ubuntu system:

# ls -l /dev/tty*
crw-rw-rw- 1 root tty     5,  0 Aug 14 08:35 /dev/tty
crw--w---- 1 root root    4,  0 Aug 14 08:35 /dev/tty0
crw------- 1 root root    4,  1 Aug 14 08:36 /dev/tty1
crw--w---- 1 root tty     4, 10 Aug 14 08:35 /dev/tty10
crw--w---- 1 root tty     4, 11 Aug 14 08:35 /dev/tty11
[...etc. ...]

Reading from such a device will give you whatever a user sitting at this terminal types, writing to such a device will produce the output at that screen. So, after making sure you are allowed to write to and read from the terminals device file you could probably direct your <stderr> output to it by

/some/process 2>/dev/ttyX

I hope this helps.

bakunin

Hello bakunin,

Thanks for your reply. The main issue I am having is reading in output from a terminal to a variable in the bash script. I know the following won't work but hopefully it can illustrate what I am trying to achieve.

 new_term_num=`xterm -e "tty > /dev/pts/0`

Regards,
Colin

First off, I'd like to warn you: using backticks is a very, very, bad idea: they are outdated, they have a lot of drawbacks, they are only supported for backwards compatibility purposes (with some scripts from the seventies) and you should forget that they exist. Instead of

`command`

use

$(command)

which does the same, only much better.

terminals don't have output - only processes have output. If you start a process you can redirect its output to <stdout> and/or <stderr> (to your terminal, a file or the device another file descriptor points to). There is no way to redirect the output of a process you don't have started.

If you want to start an xterm and display the output of some process there, you have to:

  1. create the terminal window
  2. find out the device file attached to it
  3. start your process and redirect its output to the device file in question.

I hope this helps.

bakunin