假设我们有一台安装了 Ubuntu Desktop 的计算机。其中安装了一些桌面环境 (DE),例如:Unity、Gnome、KDE、XFCE、Mate 等。
而且很少有用户拥有自己的帐户,他们经常使用计算机。每个用户都可以根据自己的意愿更改其 DE :)
对于特定用户,如何在有限的环境下通过 CLI 确定当前用户的桌面环境?
这个问题可以分解如下:
该信息存储在哪个文件中?
ssh
通过在有限环境(例如会话或)内执行的脚本读取它的最佳方法是什么cron
?如何让随机用户读取这些信息?
如果可能的话,我正在寻找一行命令解决方案。
答案1
最后,我成功地创建了脚本,该脚本在 13 种不同的桌面环境中经过几十次尝试,成功率达到 100%。
该脚本分析主题用户的每个文件的内容/proc/$PID/envion
并确定其当前的桌面环境。
$ cat ~/find-DE.bash
#!/bin/bash
# Determinate the subject user - $USR
if [ "$1" == "simple" ]; then
USR="$(id -u)"
OUTPUT="simple"
elif [ -z "${1}" ]; then
USR="$(id -u)"
OUTPUT="$2"
else
USR="$1"
OUTPUT="$2"
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[$1]
if(n>max_occ){
max_occ=n
what=$1
}else if(n==max_occ){
if(what>$1)
what=$1
}
}
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
用法:
$ ~/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
更多结果:
#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
:)
答案2
echo $DESKTOP_SESSION
显示当前使用的桌面环境。
在这里找到其他好的方法: