LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-21-2008, 01:45 AM   #1
Eilya
Member
 
Registered: Aug 2008
Posts: 52

Rep: Reputation: 15
Exclamation Distinguish Terminals


Hi dear friends,

I want to know how can we distinguish between 2 terminals. if I am int terminal 1 (/dev/pts/1) then I want to $echo "Hello" in /dev/pts/2, how can I do it?

Regards,
Eilya
 
Old 09-21-2008, 01:52 AM   #2
Bolsak
LQ Newbie
 
Registered: Sep 2008
Location: United Kingdom
Distribution: Slackware 12.0
Posts: 13

Rep: Reputation: 0
I think you can just do it anyway, like if I am on tty1 and I want to say"Hello" to tty2, I just put
Code:
echo "Hello" > /dev/tty2
 
Old 09-21-2008, 02:07 AM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
How about setting your prompt such that it includes the TTY? Example:

Code:
$ PS1='\l $ '
ttyp0 $ 
$ tty
/dev/ttyp0
ttyp0 $
The \l is the tty name in bash prompts. There are other escapes you can include. See PROMPTING in man bash.

Last edited by Mr. C.; 09-21-2008 at 03:00 AM.
 
Old 09-21-2008, 02:48 AM   #4
Eilya
Member
 
Registered: Aug 2008
Posts: 52

Original Poster
Rep: Reputation: 15
Lightbulb

Thank you Bolsak,
Quote:
How about setting your prompt such that it includes the TTY? Example:

Code:

PS1='\l $ '
$ PS1='\l $ '
ttyp0 $
$ tty
/dev/ttyp0
ttyp0 $

The \l is the tty name in bash prompts..
Please explain this code, I can not believe this
 
Old 09-21-2008, 02:58 AM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
# set the bash prompt to be the tty name followed by a $
$ PS1='\l $ '

Now we see the prompt appears to be set to the tty name followed by $
ttyp0 $

Let's check with the tty command:
$ tty
/dev/ttyp0

Yes, indeed, we are on ttyp0

ttyp0 $

and this is our prompt again.

Edit: The previous code was copy/pasted from different shell sessions. Here's another example:

Code:
$ pwd
/usr/bin
$ PS1='[\h:\w] @ \l $ '
[glacier:/usr/bin] @ ttyp0 $ tty
/dev/ttyp0
[glacier:/usr/bin] @ ttyp0 $ PS1='$ '
$ tty
/dev/ttyp0

Last edited by Mr. C.; 09-21-2008 at 03:05 AM.
 
Old 09-21-2008, 03:22 AM   #6
Eilya
Member
 
Registered: Aug 2008
Posts: 52

Original Poster
Rep: Reputation: 15
Thanks Mr.C, about that code
Quote:
echo "Hello" > /dev/tty2
can we include users in it? I mean if we want to send message to another user, what should we do??

Thanks,
Eilya
 
Old 09-21-2008, 03:33 AM   #7
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
What you are doing is something different. You are sending the output of the echo command via redirection onto the named terminal device. This just blasts characters to the device, without formatting or concern for what is currently on the screen. This is like what the write command does.

If you want to send messages, refer to the Chat thread you have open here: http://www.linuxquestions.org/questi...system-671335/
 
Old 09-21-2008, 11:10 AM   #8
Eilya
Member
 
Registered: Aug 2008
Posts: 52

Original Poster
Rep: Reputation: 15
Question

I have a question: I want to have a code like this:
Quote:
echo "hello" > /dev/pts/6
when I am in terminal 1 (/dev/pts/1), I want to just choose the terminal number, so When
Quote:
$a=6
the some how I want to have :
Quote:
$b=/dev/pts/$a
what should I do to have the paths includes: /dev/pts/
or for some distros: /dev/tty
how can I choose this paths????

Regards,
Eilya
 
Old 09-21-2008, 01:03 PM   #9
Eilya
Member
 
Registered: Aug 2008
Posts: 52

Original Poster
Rep: Reputation: 15
any ideaaaaaaaaaa?
 
Old 09-21-2008, 02:14 PM   #10
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Do something like:

ttypathbase='/dev/pts'
a=6

echo hello > $ttypathbase/$a

and change ttypathbase in your .bashrc or .bash_profile based on distribution.

I'm not sure ultimately what you want to accomplish, so its hard to give you a more comprehensive solution.
 
Old 09-21-2008, 06:50 PM   #11
Eilya
Member
 
Registered: Aug 2008
Posts: 52

Original Poster
Rep: Reputation: 15
Thanks Mr.C, so we can not use the path and just edit the number?? for example we can have the terminal path by:
Quote:
a=$tty
and it returns something like /dev/pts/1 or /dev/ttyp0 now if we could have tty command and then edit just the numbers, for example: /dev/pts/$a or /dev/ttyp$a , it could be more compatible with any distro. can we do anything for it??????

Regards,
Eilya
 
Old 09-21-2008, 07:26 PM   #12
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
You can create a function:

Code:
function ttypath {
    distro=$(uname -s)
    case $distro in
        Linux*)
            echo /dev/tty$1 ;;
        NetBSD*)
            echo /dev/tty0$1 ;;
        *)
            echo UNKNOWN ;;
    esac
}
which you call by:

Code:
$ ./ttypath 2
/dev/tty02
But beware, there are problems. There are different both TTYs and PTYs, and they have different naming conventions:
Code:
$ ls -l /dev/tty0 /dev/ptya0 
crw-rw-rw- 1 root tty 2, 176 Aug 16  2007 /dev/ptya0
crw--w---- 1 root tty 4,   0 Aug 16  2007 /dev/tty0
So you'll have to modify the function above to suit your needs and tty types, but this should give you the idea.

Last edited by Mr. C.; 09-21-2008 at 07:28 PM.
 
Old 09-22-2008, 02:35 AM   #13
Eilya
Member
 
Registered: Aug 2008
Posts: 52

Original Poster
Rep: Reputation: 15
Thanks a lot, Mr.C, As I understood there is no way for making a simple and optimum way for knowing and using the terminal path in different distros.

Thanks a lot,
Eilya
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
distinguish DDr1 or DDr2 lhrt Linux - Hardware 3 07-29-2008 02:10 AM
Distinguish between crash and reboot tukaaa Linux - General 4 11-26-2005 05:25 PM
Terminals - spying on local terminals with ssh BeatRyder Linux - Software 5 10-21-2004 01:47 AM
how to distinguish a file and a directory joeyBig Programming 2 10-01-2004 04:21 AM
how to distinguish kernel versions fobius Linux - General 2 06-24-2004 07:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration