Read input from another tty


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read input from another tty
# 1  
Old 08-14-2012
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.

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

To find the tty number of the new terminal I have

Code:
#!/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
# 2  
Old 08-14-2012
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:

Code:
# 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

Code:
/some/process 2>/dev/ttyX

I hope this helps.

bakunin
# 3  
Old 08-14-2012
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.

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

Regards,
Colin
# 4  
Old 08-14-2012
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

Code:
`command`

use

Code:
$(command)

which does the same, only much better.

Quote:
Originally Posted by colinireland
reading in output from a terminal to a variable in the bash script.
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Python: Redirecting to tty and reading from tty

In bash, you can do something like this: #!/bin/bash echo -n "What is your name? " > /dev/tty read thename < /dev/tty How can I do the same in python? I have a python script that has the following content: #!/usr/bin/python2.7 import getpass import sys import telnetlib import... (2 Replies)
Discussion started by: SkySmart
2 Replies

2. Shell Programming and Scripting

Read input from Keyboard, do not proceed if no input

Hi, I am working on a script, which requests users to enter input. Ex: read -p "Please enter your email id:" email I don't want users skipping this entry, this has to be mandatory.I dont want to proceed without input. I can do a check if variable $email is empty and proceed if not.But, i... (7 Replies)
Discussion started by: aravindadla
7 Replies

3. Shell Programming and Scripting

Using read for input over ssh

Hi, I'm using read in a script to prompt and receive input. read -r -p "Do you also want to deploy folder? " response This works fine, however, if I remotely execute the same script via ssh, at this point in the code, I don't see the message at all, but it waits for input. I could echo... (1 Reply)
Discussion started by: say170
1 Replies

4. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

5. Ubuntu

Kernel panics : trying to write / read on tiny tty driver

I'm a beginner to the Linux programming and trying my hands on some device driver examples while practising. The below code (a trimmed down version of tiny_tty.c from ldd3 book) loads perfectly using insmod and I'm able to see it in /proc/tty/drivers , /proc/modules and device nodes are getting... (1 Reply)
Discussion started by: diwsdiwa
1 Replies

6. Shell Programming and Scripting

Read Input and go BG

My script needs to get some input from the user and go as a background process and run. I have something like read input. do while ... if .. fi done can i use nohup inside the script?? (7 Replies)
Discussion started by: Antergen
7 Replies

7. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

8. Shell Programming and Scripting

Read input

I have a script. #! /bin/bash echo "Enter a word: " read word echo $word That outputs like this.. Enter a word: hello hello But how can i read on the same line the question is printed? Like this.. Enter a word: hello hello (2 Replies)
Discussion started by: cbreiny
2 Replies

9. UNIX for Dummies Questions & Answers

read from terminal/keyboard > /dev/tty

Hi, I need to provide more than one character to "> /dev/tty" through terminal/keyboard input, I have this: ok=false while do echo " Enter r1 to reformat " > /dev/tty read choice case $choice in ) echo " bla bla bla " ;; done However, in this way,... (3 Replies)
Discussion started by: Gery
3 Replies

10. UNIX for Dummies Questions & Answers

How to input escape sequences in Linux tty?

More details: Unicode, Framebuffer. I tried to press Esc and then what should follow, letters or brackets, but seems not to work. Probably i do something wrong. If somebody is familiar with escape sequences in the console, how do you do that? Thanks in advance. (4 Replies)
Discussion started by: Action
4 Replies
Login or Register to Ask a Question