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
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
It sounds to me like you want to check whether or not there is a X server to connect to.
if [ -n "$DISPLAY" ]; then : X else : no X fi
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 # konsole /dev/pts/4 $ tty # virtual terminal /dev/tty2
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.