Last active
December 23, 2015 18:59
-
-
Save xupeng/6679702 to your computer and use it in GitHub Desktop.
A relatively reliable way to get login user on Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# A relatively reliable way to get login user on Linux | |
user_entry=`getent passwd $(cat /proc/self/loginuid)` | |
if [ $? -eq 0 ]; then | |
login_user=`echo ${user_entry} | cut -d: -f1` | |
else | |
login_user=${SUDO_USER:-${LOGNAME}} | |
if [ "${login_user}" = "" ]; then | |
login_user=`id -urn` | |
fi | |
fi | |
echo ${login_user} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment