11

A few of my scripts (keymap changing, public key adding) need to act differently when in a virtual console vs. in an xterm. What's the real code for

#!/bin/ksh
if [[ in_a_virtual_console ]]; then
...
else
...
fi
6

It sounds to me like you want to check whether or not there is a X server to connect to.

Something like:

if [ -n "$DISPLAY" ]; then
  : X
else
  : no X
fi
  • 1
    I've never been in the position of having three perfect answers on stackexchange. – John Baber-Lucero Sep 3 '12 at 21:05
9

If you meant to distinguish interactive from noninteractive shells, use test's inbuilt support for detecting it (not sure if ksh has it, but the binary from coreutils supports it too):

test -t 0 # will return 0 for interactive shells

If you really meant whether you're on a tty/pty/pts or something else, parse the output from tty:

$ tty # konsole
/dev/pts/4
$ tty # virtual terminal
/dev/tty2
  • I've never been in the position of having three perfect answers on stackexchange. – John Baber-Lucero Sep 3 '12 at 21:01
7

This will depend on the operating system. For Linux, you could do this:

#!/bin/ksh

if [ "$TERM" == "linux" ]; then
  echo "I'm in a virtual console"
else
  echo "I'm not in a virtual console"
fi

This, of course, assumes that you have not changed the TERM environment variable yourself, which is automatically set to linux only when you log into a virtual console (under Ubuntu it is set to xterm in console applications, but this may vary between OSes). Check the contents of TERM in the various applications you're using, and set as appropriate.

  • I've never been in the position of having three perfect answers on stackexchange. – John Baber-Lucero Sep 3 '12 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.