如何在bash脚本中检测桌面环境?

Ser*_*ndt 25 bash desktop-environment bash-script

我正在编写一个bash脚本,需要知道哪个桌面环境(XFCE、Unity、KDE、LXDE、Mate、Cinnamon、GNOME2、GNOME3...)正在运行。

我怎样才能获得这些信息?

Gra*_*eme 12

检查 的主要问题DESKTOP_SESSION是它是由显示管理器而不是桌面会话设置的,并且存在不一致的情况。对于lightdmDebian,这些值来自/usr/share/xsessions/. DESKTOP_SESSION如果在登录时做出特定选择,则反映桌面环境,但lightdm-xsession始终使用默认会话。

GDMSESSION是另一种选择,但似乎也有类似的困境(与DESKTOP_SESSION我的价值相同)。

XDG_CURRENT_DESKTOP看起来是一个不错的选择,但目前它不在XDG 标准中,因此并不总是得到实施。有关问题的讨论,请参见此处。这个答案显示了它在不同发行版/桌面上的值,我也可以确认它目前在 XFCE 上不可用。

XDG_CURRENT_DESKTOP不存在的合理回退是尝试XDG_DATA_DIRS。如果桌面环境的数据文件安装在以其名称命名的目录中,这种方法应该可行。这有望适用于所有发行版/桌面!

以下(使用 GNU grep)XFCE、KDE ​​和 Gnome 测试:

echo "$XDG_DATA_DIRS" | grep -Eo 'xfce|kde|gnome'
Run Code Online (Sandbox Code Playgroud)

POSIX 兼容:

echo "$XDG_DATA_DIRS" | sed 's/.*\(xfce\|kde\|gnome\).*/\1/'
Run Code Online (Sandbox Code Playgroud)

结合检查XDG_CURRENT_DESKTOP

if [ "$XDG_CURRENT_DESKTOP" = "" ]
then
  desktop=$(echo "$XDG_DATA_DIRS" | sed 's/.*\(xfce\|kde\|gnome\).*/\1/')
else
  desktop=$XDG_CURRENT_DESKTOP
fi

desktop=${desktop,,}  # convert to lower case
echo "$desktop"
Run Code Online (Sandbox Code Playgroud)


slm*_*slm 10

方法 #1 - $DESKTOP_SESSION

我想你可以通过询问环境变量来找出答案$DESKTOP_SESSION。我并不完全肯定它的支持范围有多广,但在我有限的测试中,它似乎可以在 Fedora 和 Ubuntu 上使用。

$ echo $DESKTOP_SESSION
gnome
Run Code Online (Sandbox Code Playgroud)

另一个选择是$XDG_SESSION_DESKTOP变量。

方法#2 - wmctrl

还有这种方法利用wmctrl.

$ wmctrl  -m
Name: GNOME Shell
Class: N/A
PID: N/A
Window manager's "showing the desktop" mode: N/A
Run Code Online (Sandbox Code Playgroud)

参考


小智 5

您可以使用此bash 脚本。它可以检测桌面环境名称和版本。

#!/bin/bash

function detect_gnome()
{
    ps -e | grep -E '^.* gnome-session$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`gnome-session --version | awk '{print $2}'`
    DESKTOP="GNOME"
    return 1
}

function detect_kde()
{
    ps -e | grep -E '^.* kded4$' > /dev/null
    if [ $? -ne 0 ];
    then
        return 0
    else    
        VERSION=`kded4 --version | grep -m 1 'KDE' | awk -F ':' '{print $2}' | awk '{print $1}'`
        DESKTOP="KDE"
        return 1
    fi
}

function detect_unity()
{
    ps -e | grep -E 'unity-panel' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`unity --version | awk '{print $2}'`
    DESKTOP="UNITY"
    return 1
}

function detect_xfce()
{
    ps -e | grep -E '^.* xfce4-session$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`xfce4-session --version | grep xfce4-session | awk '{print $2}'`
    DESKTOP="XFCE"
    return 1
}

function detect_cinnamon()
{
    ps -e | grep -E '^.* cinnamon$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`cinnamon --version | awk '{print $2}'`
    DESKTOP="CINNAMON"
    return 1
}

function detect_mate()
{
    ps -e | grep -E '^.* mate-panel$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi
    VERSION=`mate-about --version | awk '{print $4}'`
    DESKTOP="MATE"
    return 1
}

function detect_lxde()
{
    ps -e | grep -E '^.* lxsession$' > /dev/null
    if [ $? -ne 0 ];
    then
    return 0
    fi

    # We can detect LXDE version only thru package manager
    which apt-cache > /dev/null 2> /dev/null
    if [ $? -ne 0 ];
    then
    which yum > /dev/null 2> /dev/null
    if [ $? -ne 0 ];
    then
        VERSION='UNKNOWN'
    else
        # For Fedora
        VERSION=`yum list lxde-common | grep lxde-common | awk '{print $2}' | awk -F '-' '{print $1}'`
    fi
    else    
    # For Lubuntu and Knoppix
    VERSION=`apt-cache show lxde-common /| grep 'Version:' | awk '{print $2}' | awk -F '-' '{print $1}'`
    fi
    DESKTOP="LXDE"
    return 1
}

function detect_sugar()
{
    if [ "$DESKTOP_SESSION" == "sugar" ];
    then
    VERSION=`python -c "from jarabe import config; print config.version"`
    DESKTOP="SUGAR"
    else
    return 0
    fi
}


DESKTOP="UNKNOWN"
if detect_unity;
then
    if detect_kde;
    then
    if detect_gnome;
    then
        if detect_xfce;
        then
        if detect_cinnamon;
        then
            if detect_mate;
            then
            if detect_lxde;
            then
                detect_sugar
            fi
            fi
        fi
        fi
    fi
    fi
fi


if [ "$1" == '-v' ];
then
    echo $VERSION
else
    if [ "$1" == '-n' ];
    then
    echo $DESKTOP
    else
    echo $DESKTOP $VERSION
    fi
fi
Run Code Online (Sandbox Code Playgroud)