如何获得拥有当前活动的GUI会话的用户名? [英] How do I get the username owning the currently active gui session?
问题描述
我发布了一些可自动挂载的脚本并在Linux中卸载磁盘,现在我得到了一个请求使磁盘归当前登录用户所有.也就是说,当前持有gui的用户已被占用(在这种情况下,我们不在乎终端用户).
I published some scripts to automatically mount and unmount disks in Linux, and now I got a request to make the disks owned by the currently logged in user. That is, the user currently holding the gui occupied (we don't care about terminal users in this case).
我将如何获得用户名?可能会有许多不同的窗口管理器,因此,如果不存在与wm无关的解决方案,则可能需要根据wm的不同解决方案.
How would I go about getting hold of the username? There could be a multitude of different window managers, so different solutions depending on wm might be needed, if no wm-independant solution exists.
首先,仅针对Gnome的解决方案就足够了.
For a start, a solution just targeting Gnome might be enough.
该脚本正在任何用户会话外部运行(由udev触发).
推荐答案
如果运行的是bash脚本,则当前登录用户的用户名应存储为环境变量$USER
或$USERNAME
. env
命令将为您提供当前shell中所有环境变量的列表.
If you are running a bash script, the username of the currently logged-in user should be stored as an environment variable, $USER
or $USERNAME
. The env
command will give you a list of all environment variables in the current shell.
现在,如果要使用当前用户的用户ID,请使用id
命令.
Now if you want the user id of the current user, use the id
command.
这篇关于如何获得拥有当前活动的GUI会话的用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!