-
Notifications
You must be signed in to change notification settings - Fork 70
Finds user's home directory with dscl (Issue #73) #132
base: main
Are you sure you want to change the base?
Conversation
Adds compatibility for users that have their home directory in a non-standard location.
Hello Thanks for this commit but this way of doing things will break if the user home folder is Eventually using Thanks |
@@ -8,10 +8,11 @@ | |||
|
|||
|
|||
loggedInUser=$(/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");') | |||
homeDir=$(/usr/bin/dscl . read /Users/${loggedInUser} NFSHomeDirectory | /usr/bin/awk '{print $2}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the NFSHomeDirectory
is set to /Volumes/Users HD/Users/ygi
the awk
command will only return /Volumes/Users
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, good catch. I'll do some testing and resubmit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@canalnoises You could do this:
homedir="$(/usr/bin/dscl -plist . read "/Users/${loggedInUser}" NFSHomeDirectory | /usr/bin/xpath -q -e '//key[text()="dsAttrTypeStandard:NFSHomeDirectory"][1]/following-sibling::array[1]/string[1]/text()')"
Explanation of the parts follows.
/usr/bin/dscl -plist . read "/Users/${loggedInUser}" NFSHomeDirectory
This will output a plist that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>dsAttrTypeStandard:NFSHomeDirectory</key>
<array>
<string>/Volumes/Users HD/Users/ygi</string>
</array>
</dict>
</plist>
We want to get the first/only value of dsAttrTypeStandard:NFSHomeDirectory
, so we pass it into xpath
with the following query:
//key[text()="dsAttrTypeStandard:NFSHomeDirectory"][1]/following-sibling::array[1]/string[1]/text()
Breaking the query down:
//key[text()="dsAttrTypeStandard:NFSHomeDirectory"][1]
This selects the first <key>
element whose content is dsAttrTypeStandard:NFSHomeDirectory
.
/following-sibling::array[1]
This selects the first <array>
element that follows the selected <key>
element.
/string[1]/text()
This returns the content of the first <string>
element that descends from the selected <array>
element.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, you could do this:
homeDir="$(/usr/bin/dscl . read "/Users/${loggedInUser}" NFSHomeDirectory | /usr/bin/sed 's/^NFSHomeDirectory: //')"
Whichever you do, you should put quotes around both the $()
and the userpath, in case either the result or the userpath contains spaces.
Adds compatibility for users that have their home directory in a non-standard location.