Created
October 27, 2021 18:08
-
-
Save micahculpepper/3a9533b4967647765842030139209db3 to your computer and use it in GitHub Desktop.
Find the logged-in user on MacOS
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
#!/usr/bin/bash | |
RUID=$(id -ru) | |
RGID=$(id -rg) | |
# Find logged in user via the Python -> Objective C bridge | |
# Docs: https://pythonhosted.org/pyobjc/apinotes/SystemConfiguration.html | |
output=$(cat << EOF | /usr/bin/python - | |
import sys | |
from SystemConfiguration import SCDynamicStoreCopyConsoleUser | |
res = SCDynamicStoreCopyConsoleUser(None, None, None) | |
if not res: | |
sys.exit(1) | |
username, uid, gid = res | |
if (not username) or (username == "loginwindow"): | |
sys.exit(1) | |
print("RUID={}\nRGID={}".format(uid, gid)) | |
EOF | |
2>/dev/null) | |
if [ $? = 0 ]; then | |
eval "$output" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
even if you run this script via sudo, you'll still get the RUID and RGID of yourself, not just 0