[OT] how can a bash script determine if it's running in an xterm???

Nifty Hat Mitch mitch48 at sbcglobal.net
Fri Jul 9 02:53:21 UTC 2004


On Wed, Jul 07, 2004 at 12:51:05PM -0400, Joe(theWordy)Philbrook wrote:
> 
> 
> Actually there are three states I want to base some conditional execution
> on. 
> 
> I can do this now based on feeding the script a command-line argument.
> But I'm wanting to have the script itself determine if:
> 
> 1) It's running in a VT such as found in runlevel 3 or via ctrl+alt+F1
>    in runlevel 5.

The tty command should work here.
The  ctrl+alt+F1 group look like:

     # tty
      /dev/tty1

     tty2, tty3, tty4... etc.

> 2) It's running in an "xterm", "konsole", "aterm", or (ANY other x based
>    terminal window).

For normal xterm, konsole you will see /dev/pts/nn for these and ssh
login:

  $ tty
  /dev/pts/26

It is possible to see something different if you have old ptys
configured (takes work).  The openpty() getpty() call will 
return one of the standard tty's.  Even inside of emacs I see
  $ tty
  /dev/pts/28

> 3) It's not attached to a terminal or VT (such as might happen if the
>    script was called from alt+F2 run prompt).

See /etc/bashrc
   # are we an interactive shell?
   if [ "$PS1" ]; then
       case $TERM in
           xterm*)
   ....etc.

The run level can be tested by looking at the output of "who -r: or
"runlevel".

You may also have other valuable hooks in the environment.
see "set", "printenv", "env".  Look for stuff like.
   SSH_TTY=/dev/pts/27
   TERM=xterm
   DISPLAY=:0.0
   SHELL=/bin/bash
   COLORTERM=gnome-terminal

It is possible for users to set some of these in unexpected ways so it
is worth testing and not trusting environment variables for security
purposes.

For example you can explore the environment given a process
when a script is called from alt+F2 run prompt by having 
a script do something as simple as "env > /tmp/show-my-env"


-- 
	T o m  M i t c h e l l 
	/dev/null the ultimate in secure storage.





More information about the fedora-list mailing list