You are not logged in.

#1 2010-07-13 16:45:17

disturb
Member
Registered: 2009-12-18
Posts: 70

How to check wether the current shell is running in a virtual terminal

I'm writing a simple bash function, which to be really useful, should be able determine wether the current shell is running in a virtual terminal or a tty.
Checking $TERM doesn't cut it, since it's not consistent, and I could be using screen, tmux or whatever...

Is there a way to do that ?

Offline

#2 2010-07-13 16:56:10

Bralkein
Member
Registered: 2004-10-26
Posts: 354

Re: How to check wether the current shell is running in a virtual terminal

I think checking $DISPLAY might do what you want

Offline

#3 2010-07-13 17:07:08

jac
Member
From: /home/jac
Registered: 2009-05-19
Posts: 431
Website

Re: How to check wether the current shell is running in a virtual terminal

How about tty? Just look to see if it gives back something like /dev/tty* or /dev/pts/*

Offline

#4 2010-07-13 19:22:10

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: How to check wether the current shell is running in a virtual terminal

By "shell" you mean f.e. bash?

[karol@black pbase]$ ps aux | grep bash | grep -v grep
karol     2823  0.0  0.1   5344  1944 tty1     S    01:05   0:00 bash
karol     2952  0.0  0.2   5456  2044 pts/5    S    01:18   0:00 bash
karol     4892  0.0  0.1   5344  1712 pts/7    S+   21:28   0:00 bash

Am I totally missing the point?

Offline

#5 2010-07-13 19:35:19

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: How to check wether the current shell is running in a virtual terminal

@jac: "tty" still gives /dev/pts/* from within screen and tmux.  Anyway, I'm not sure there's a good way to do this.  Why do you want to?

Offline

#6 2010-07-13 23:22:52

PirateJonno
Forum Fellow
From: New Zealand
Registered: 2009-04-13
Posts: 372

Re: How to check wether the current shell is running in a virtual terminal

karol wrote:

By "shell" you mean f.e. bash?

For future reference, "for example" is traditionally abbreviated to "e.g."

karol wrote:

Am I totally missing the point?

Yes, since he wants to know the current shell, not a list of the ones running smile


"You can watch for your administrator to install the latest kernel with watch uname -r" - From the watch man page

Offline

#7 2010-07-13 23:33:06

milomouse
Member
Registered: 2009-03-24
Posts: 940
Website

Re: How to check wether the current shell is running in a virtual terminal

i used to use: ps $$, but i found this to be better:

ps --no-headers --format tty,comm $$

not sure if that's what you're looking for

EDIT: my mistake, didn't full read.
EDIT2: this code snippet should produce desired results, if not a variable of it.

Last edited by milomouse (2010-07-13 23:59:37)

Offline

#8 2010-07-13 23:35:01

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: How to check wether the current shell is running in a virtual terminal

> not sure if that's what you're looking for
I don't think so - OP wants to know whether the script (or shell) is running in tty on under X - I think.

Offline

#9 2010-07-13 23:38:37

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: How to check wether the current shell is running in a virtual terminal

[karol@black ~]$ ps aux | grep bash | grep grep
karol     5491  0.0  0.0   4332   692 pts/5    S+   01:46   0:00 grep bash

now I know the above command was run on  pts/5.

Last edited by karol (2010-07-13 23:40:16)

Offline

#10 2010-07-13 23:43:46

milomouse
Member
Registered: 2009-03-24
Posts: 940
Website

Re: How to check wether the current shell is running in a virtual terminal

@karol: i'm pretty sure that's the same thing mine does but without grep. did you try it? open a few terminals and run that command in each one. it will show pts*/tty* of current shell. or try it in a script and do the same thing.

Offline

#11 2010-07-13 23:50:28

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: How to check wether the current shell is running in a virtual terminal

@ milomouse
Cool, you saved me reading through the ps manpage - I knew there was a cleaner way to do it.
Your original post had 'ps --no-headers --format comm $$' and that didn't work, 'ps --no-headers --format tty,comm $$' does beautifully.

Offline

#12 2010-07-13 23:52:16

milomouse
Member
Registered: 2009-03-24
Posts: 940
Website

Re: How to check wether the current shell is running in a virtual terminal

Yeah, I made a quick edit probably after you read it, once I realized that the tty/pts was the important part. Glad it works, at least for us. We'll see if it's what op needs. :}

Offline

#13 2010-07-13 23:54:53

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: How to check wether the current shell is running in a virtual terminal

Next time add

Edit: I fixed the command, now it works

instead of

EDIT: my mistake, didn't full read.

Offline

#14 2010-07-13 23:58:55

milomouse
Member
Registered: 2009-03-24
Posts: 940
Website

Re: How to check wether the current shell is running in a virtual terminal

Not to stray too far off topic, but I'm still not sure if that "works" for the op, but I'll edit my post anyway. Thanks for pointing that out.

Offline

#15 2010-07-14 00:43:36

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: How to check wether the current shell is running in a virtual terminal

This is silly. Why are you guys emulating the tty command with ps?

In regards to OP's question there is no way for a program running under screen to figure out what type of terminal screen is running in. At least there is no straight forward way and it doesn't matter as some of the linux console's features would be blocked to programs.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#16 2010-07-14 15:25:00

PirateJonno
Forum Fellow
From: New Zealand
Registered: 2009-04-13
Posts: 372

Re: How to check wether the current shell is running in a virtual terminal

fsckd wrote:

In regards to OP's question there is no way for a program running under screen to figure out what type of terminal screen is running in. At least there is no straight forward way and it doesn't matter as some of the linux console's features would be blocked to programs.

What was wrong with checking if DISPLAY is set (mentioned above)?


"You can watch for your administrator to install the latest kernel with watch uname -r" - From the watch man page

Offline

#17 2010-07-14 15:29:39

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: How to check wether the current shell is running in a virtual terminal

edit: DISPLAY will be set if screen were detached from a virtual terminal and reattached to the linux console. So no, not a perfect solution.

Last edited by fsckd (2010-07-14 15:35:22)


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

Board footer

Powered by FluxBB