Question

Can a shell script determine whether the user is running a graphical interface at the same tty in which the user is working?

Maybe this isn't even possible?

EDIT:

I'm actually trying to write a shell script that displays a GUI (by using zenity/yad) when the user is running a window manager on the same tty as the user is working on. Otherwise, the script falls back to a plain command line interface.

Was it helpful?

Solution

Try to check the variable TERM or perhaps more precisely by checking the output of tset -q. The type of terminal would depend on it. In a normal console, it's just Linux while in most terminals it is xterm.

Other things would be to check the variable $DISPLAY. Most of the time it's set when you're running on a GUI terminal.

OTHER TIPS

One sure way is to throw out some GUI process that will terminate right away, and check the exit status. The xterm is usually available (although the latest Linux Mint did not have it by default) and can be made to exit right away:

xterm -iconic -e echo test && echo "Has a display"

The -iconic should prevent the xterm from flashing on the display (I hope that is a general option).

Since your environment has zenity, it will also return a bad status if the display cannot be used, and output an error message. You can detect this in bash:

echo "" | zenity --progress --text "Display test" --auto-close 2> /dev/null
if [ $? -eq 0 ]
then 
   echo "has display"
fi

Unfortunately the progress dialog flashes on the display for an instant. This has the added benefit of detecting if zenity itself is not available.

I have used the xterm approach in the past.

What you really want to test is whether or not a tty or pseudotty is present.

On linux, use the command tty. It will return 0 and output the tty file location, or return 1 (false) and print not a tty when it is not a tty.

The tset -q command can return a TERM (for instance linux in my case) regardless.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top