Reputation: 5024
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!
Upvotes: 9
Views: 3073
Reputation: 90571
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.
Upvotes: 9
Reputation: 158
Try this :
echo $XDG_CURRENT_DESKTOP
Or this :
echo $DESKTOP_SESSION
Upvotes: 2
Reputation: 3237
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.
Upvotes: 3
Reputation: 655
Check if DISPLAY is set in the environment. If it is, you have an Xserver running. If not, you don't. Pretty sure that wayland sets it too.
Upvotes: 0