Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect availability of GUI in Bash/Shell?

I'm writing a CLI in NodeJS. As I can easily run bash/shell commands using the child_process, I'd like to know the easiest most cross platform way to detect GUI availability in bash/shell.

Thanks!

like image 555
Dev Avatar asked Mar 05 '19 05:03

Dev


People also ask

Does bash have a GUI?

Many programming languages have a way of a coding graphical interface. Luckily, there is a way to code a GUI for shell scripts on bash in the script itself.

What is GUI bash?

In fact, you are able to include GUI (Graphical User Interface) based input/output components into your next bash script using the Zenity command line tool which helps us to display GTK dialog boxes. Furthermore, Native GUI notifications can be displayed using the notify-send command line tool.

How do I know if GNOME is installed on Linux?

You can determine the version of GNOME that is running on your system by going to the About panel in Settings. Open the Activities overview and start typing About. A window appears showing information about your system, including your distribution's name and the GNOME version.


2 Answers

On macOS, there's not a clearly appropriate way to check this from a shell, as such. There's a programmatic way, and we can use an interpreted language to take advantage of that.

Here's a little script that outputs one of three states, Mac GUI, Mac non-GUI, or X11:

#!/bin/bash

check_macos_gui() {
  command -v swift >/dev/null && swift <(cat <<"EOF"
import Security
var attrs = SessionAttributeBits(rawValue:0)
let result = SessionGetInfo(callerSecuritySession, nil, &attrs)
exit((result == 0 && attrs.contains(.sessionHasGraphicAccess)) ? 0 : 1)
EOF
)
}

if [ "$(uname)" = "Darwin" ]; then
  if check_macos_gui; then
    echo "Mac GUI session"
  elif [ -n "$DISPLAY" ]; then
    echo "Mac X11 GUI session"
  else
    echo "Mac non-GUI session"
  fi
elif [ -n "$DISPLAY" ]; then
  echo "X11 GUI session"
fi

Macs can have an X server installed, in which case DISPLAY is defined. However, I don't know if your Electron app will work properly in that configuration. So, I detected it separately.

like image 80
Ken Thomases Avatar answered Oct 26 '22 23:10

Ken Thomases


Here is a working example:

if [ x$DISPLAY != x ] ; then
  echo "GUI Enabled"

else
  echo "GUI Disabled"

fi

All this does is checks the $DISPLAY variable.

like image 25
xilpex Avatar answered Oct 26 '22 21:10

xilpex