Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining if the user is running a graphical interface at the present tty

Tags:

bash

shell

tty

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.


2 Answers

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.

like image 167
konsolebox Avatar answered Sep 07 '25 20:09

konsolebox


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.

like image 25
Steve Zobell Avatar answered Sep 07 '25 19:09

Steve Zobell