You are not logged in.
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
I think checking $DISPLAY might do what you want
Offline
How about tty? Just look to see if it gives back something like /dev/tty* or /dev/pts/*
Offline
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
@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
By "shell" you mean f.e. bash?
For future reference, "for example" is traditionally abbreviated to "e.g."
Am I totally missing the point?
Yes, since he wants to know the current shell, not a list of the ones running
"You can watch for your administrator to install the latest kernel with watch uname -r" - From the watch man page
Offline
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
> 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
[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
@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
@ 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
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
Next time add
Edit: I fixed the command, now it works
instead of
EDIT: my mistake, didn't full read.
Offline
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
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
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
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