Por fim, consegui criar um script que tem 100% de sucesso com várias dezenas de tentativas em 13 diferentes ambientes de área de trabalho.
O script analisa o conteúdo de cada arquivo /proc/$PID/envion
do usuário do assunto e determina qual é o ambiente atual da área de trabalho.
$ cat ~/find-DE.bash
#!/bin/bash
# Determinate the subject user - $USR
if [ "" == "simple" ]; then
USR="$(id -u)"
OUTPUT="simple"
elif [ -z "" ]; then
USR="$(id -u)"
OUTPUT=""
else
USR=""
OUTPUT=""
fi
[ "$USR" == "$(id -u)" ] && SUDO="" || SUDO="sudo"
# Get the most frequent value of any array - https://stackoverflow.com/a/43440769/6543935
get_frequent(){
awk 'BEGIN{FS=" "} {for(i=1;i<=NF;i++) print $i}' | \
awk '
{
n=++hsh[]
if(n>max_occ){
max_occ=n
what=
}else if(n==max_occ){
if(what>)
what=
}
}
END { print what }
'
}
# Get the numbers of all $USR's processes
PS='pgrep -U "${USR}"'
# Get the values of $XDG_CURRENT_DESKTOP, $GDMSESSION, $DESKTOP_SESSION from each "/proc/$ProcessNumber/environ" file
for PN in $PS; do
XDG_CURRENT_DESKTOP+=$($SUDO sed -zne 's/^XDG_CURRENT_DESKTOP=//p' "/proc/$PN/environ" 2>/dev/null; echo " ")
GDMSESSION+=$($SUDO sed -zne 's/^GDMSESSION=//p' "/proc/$PN/environ" 2>/dev/null; echo " ")
DESKTOP_SESSION+=$($SUDO sed -zne 's/^DESKTOP_SESSION=//p' "/proc/$PN/environ" 2>/dev/null; echo " ")
done
# Get the most frequent name of any desctop environment
# This is a way to find the current DE when it is changed a little bit ago
XDG_CURRENT_DESKTOP=$(echo -e ${XDG_CURRENT_DESKTOP[@]} | get_frequent)
GDMSESSION=$(echo -e ${GDMSESSION[@]} | get_frequent)
DESKTOP_SESSION=$(echo -e ${DESKTOP_SESSION[@]} | get_frequent)
# Print the output values
if [ "$OUTPUT" == "simple" ]; then
echo "${XDG_CURRENT_DESKTOP[@],,}" | sed 's/\-.*//'
else
echo "user: $(id -n -u $USR)"
echo "uid: $USR"
echo "XDG_CURRENT_DESKTOP: ${XDG_CURRENT_DESKTOP[@]^}"
echo "GDMSESSION: ${GDMSESSION[@]^}"
echo "DESKTOP_SESSION: ${DESKTOP_SESSION[@]^}"
fi
Uso:
$ ~/find-DE.bash
user: spas
uid: 1000
XDG_CURRENT_DESKTOP: GNOME-Classic:GNOME
GDMSESSION: Gnome-classic
DESKTOP_SESSION: Gnome-classic
$ ~/find-DE.bash simple
gnome
$ ~/find-DE.bash 1001
user: guest
uid: 1001
XDG_CURRENT_DESKTOP: Unity
GDMSESSION: Ubuntu
DESKTOP_SESSION: Ubuntu
$ time ~/find-DE.bash 1001 simple
unity
real 0m1.587s
user 0m0.536s
sys 0m0.400s
Mais resultados:
#1
XDG_CURRENT_DESKTOP: Unity
GDMSESSION: Ubuntu
DESKTOP_SESSION: Ubuntu
#2
XDG_CURRENT_DESKTOP: GNOME
GDMSESSION: Gnome
DESKTOP_SESSION: Gnome
#3
XDG_CURRENT_DESKTOP: GNOME-Classic:GNOME
GDMSESSION: Gnome-classic
DESKTOP_SESSION: Gnome-classic
#4
XDG_CURRENT_DESKTOP: LXDE
GDMSESSION: LXDE
DESKTOP_SESSION: LXDE
#5
XDG_CURRENT_DESKTOP: LXDE
GDMSESSION: Lubuntu
DESKTOP_SESSION: Lubuntu
#6
XDG_CURRENT_DESKTOP: LXDE
GDMSESSION: Lubuntu-Netbook
DESKTOP_SESSION: Lubuntu-Netbook
#7
XDG_CURRENT_DESKTOP: GNOME
GDMSESSION: Openbox
DESKTOP_SESSION: Openbox
#8
XDG_CURRENT_DESKTOP: KDE
GDMSESSION: Plasma
DESKTOP_SESSION: Plasma
#9
XDG_CURRENT_DESKTOP: XFCE
GDMSESSION: Xfce
DESKTOP_SESSION: Xfce
#10
XDG_CURRENT_DESKTOP: XFCE
GDMSESSION: Xubuntu
DESKTOP_SESSION: Xubuntu
#11
XDG_CURRENT_DESKTOP: X-Cinnamon
GDMSESSION: Cinnamon
DESKTOP_SESSION: Cinnamon
#12
XDG_CURRENT_DESKTOP: X-Cinnamon
GDMSESSION: Cinnamon2d
DESKTOP_SESSION: Cinnamon2d
#13
XDG_CURRENT_DESKTOP: MATE
GDMSESSION: Mate
DESKTOP_SESSION: Mate
:)