问题
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).
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.
For a start, a solution just targeting Gnome might be enough.
The script is running outside of any user session (triggered by udev).
回答1:
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.
Now if you want the user id of the current user, use the id
command.
回答2:
w -hs | grep tty7 | cut -f1 -d ' '
seems to work if your xserver is attached to tty7
(modify accordingly).
回答3:
Gnome should start an process called gdm (Gnome Display Manager).
If you grep for it, it should pass you the user who is running it.
ps aux | grep gdm
来源:https://stackoverflow.com/questions/17996707/how-do-i-get-the-username-owning-the-currently-active-gui-session