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!
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With