PDA

View Full Version : [Bash] Finding Current Desktop Environment



dodle
April 15th, 2010, 07:47 AM
I'm trying to write a script that will find out which desktop is being used and launch the corresponding "gksu" or "kdesu". According to what I have been able to find, i can use $DESKTOP_SESSION, but this variable is set by GDM. So if someone logs in without using GDM then it won't work.

Is there an environment variable that is specific to each desktop? That way I can do something like this:

#! /bin/bash

if [ -n "${KDE_VARIABLE+x}" ]; then
echo The current session is KDE

elif [ -n "${GNOME_VARIABLE+x}" ]; then
echo The current session is Gnome

elif [ -n "${XFCE_VARIABLE+x}" ]; then
echo The current session is XFCE
fi
Also: I've heard people talking about a command "xdg-su" that is supposed to replace "kdesu" and "gksu", but it isn't installed on my system and does not seem to be included with xdg-utils.

Edit: Or is it a better idea to run through the processes and find desktop specific processes like "gnome-session" and "kde-session"? I don't know how to do this yet, so any advice is welcome. Also, what would a process look like for other DEs like XFCE, "xfce-session"?

soltanis
April 15th, 2010, 08:43 AM
I would run through the processes, when it comes down to it, that's your most reliable indicator. You can use awk or grep to find the process you are looking for, e.g.



ps -e | awk '/gnome/ { print "gksu" } /kde/ { print "kdesu" }'

nvteighen
April 15th, 2010, 03:51 PM
During LaRoza's sysres days, what we did was to:

1) Check if Kicker was running. If true, then it was KDE.


ps -ef | grep "[k]icker" >/dev/null 2>&1


2) If (1) was false, we checked if X was running by testing if DISPLAY was defined. If it was, we assumed that a GTK+ based/capable DE was running.

3) If everything failed, we defaulted to CLI.

I know it was a quite risky approach, but it worked in the cases we needed. Maybe you can elaborate this a bit more and adapt it to your needs.

Reiger
April 15th, 2010, 04:01 PM
KDE sets the following variables:


KDE_FULL_SESSION
KDE_MULTIHEAD
KDE_SESSION_UID
KDE_SESSION_VERSION

diesch
April 15th, 2010, 06:43 PM
From xdg-open:


detectDE()
{
if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
elif xprop -root _DT_SAVE_MODE | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
fi
}