3

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.

5

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
}
2
  • error: must be on local machine to add or remove hosts. – Erik Aronesty Jun 26 '15 at 17:41
  • 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. – Oh My Goodness Feb 11 '16 at 21:51
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.