in reply to How to detect X?

If an X server is available, it can be reached/detected via the %ENV hash:

if ($ENV{DISPLAY}) { print "Found X server at $ENV{DISPLAY}\n"; };

Keep in mind that it might be practical if the UI-less variant can be used even when there is an X server available; examples are shell-scripts and pipes...

Replies are listed 'Best First'.
Re^2: How to detect X?
by blazar (Canon) on Feb 21, 2005 at 09:33 UTC
    If an X server is available, it can be reached/detected via the %ENV hash:
    if ($ENV{DISPLAY}) { print "Found X server at $ENV{DISPLAY}\n"; };
    Yes, I knew, but a random look at the pages I found with google seemed to suggest that this may be highly unreliable, in particular that it may remain set also where not available. I have quickly checked here on a few machines and I have the impression that it is indeed so.
    Keep in mind that it might be practical if the UI-less variant can be used even when there is an X server available; examples are shell-scripts and pipes...
    Indeed this is why I wrote
    I would like to write a script that supports both a cmd line UI and a GUI (Tk), and in addition to cmd-line switches to activate one or the other[...]
    In the first place.
Re^2: How to detect X?
by Anonymous Monk on Feb 21, 2005 at 10:03 UTC
    That would not be very reliable. DISPLAY isn't always set, even if an X server is available. And while you shouldn't, it's not uncommon for an 'DISPLAY=...' line to be found in a .profile (usually because the GUI setup doesn't set DISPLAY properly) - my .profile had such a line for a long time.

    Probably the best way is to just try to launch the GUI version of the application. If it works, it works, if it doesn't, nothing is displayed and you fall back on the non GUI version.