Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question

3 Answers 3

up vote 5 down vote accepted

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
share|improve this answer
1  
I've never been in the position of having three perfect answers on stackexchange. –  John Baber Sep 3 '12 at 21:05

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
share|improve this answer
    
I've never been in the position of having three perfect answers on stackexchange. –  John Baber Sep 3 '12 at 21:01

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.

share|improve this answer
    
I've never been in the position of having three perfect answers on stackexchange. –  John Baber Sep 3 '12 at 21:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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