Is there a way to detect if the display is valid in a controlled way? For example if I set $DISPLAY=anydisplay
or $DISPLAY=0.0
, is there a way to check if anydisplay
or 0.0
is a valid display which I have access to? I am writing a program that is intended to be launched from a bash script and the idea is to warn the user if their display is not valid and run the program in 'console' mode.
2 Answers
xhost
is a minimalistic way to probe display accessibility.
#!/usr/bin/env bash
xhost +si:localuser:$( whoami ) >&/dev/null && {
echo "GUI"
myapp --gui
} || {
echo "console"
myapp --console
}
-
-
what's the complaint, exactly? As written, the code works (that message indicates display is accessible, and exit code is success) and conceals the the spurious error message. Feb 11, 2016 at 21:51
You could just make the script exit with an informative error message. If you try to launch a graphical operation and the display is not accessible, you will get an error message. So, just attempt to launch whatever it is that you are doing graphically and if you can't exit and tell the user to re-run the script with another argument. For example:
#!/usr/bin/env bash
xterm 2>/dev/null ||
(echo "The display $DISPLAY is not accessible."
echo "Please run the script again using the '--no-gui' optiion" &&
exit 1)
Just replace xterm
with whatever function you use to launch your GUI.