Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Finds user's home directory with dscl (Issue #73) #132

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

canalnoises
Copy link

Adds compatibility for users that have their home directory in a non-standard location.

Adds compatibility for users that have their home directory in a non-standard location.
@ygini
Copy link
Member

ygini commented Dec 21, 2020

Hello

Thanks for this commit but this way of doing things will break if the user home folder is /Volumes/Users HD/Users/ygi.

Eventually using ~ygi/Library… should be a better solution, can you try on your setup and check if it works (all my setup with non standard home folder are gone now).

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}')
Copy link
Member

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.

Copy link
Author

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.

Copy link

@elyscape elyscape Oct 13, 2021

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.

Copy link

@elyscape elyscape Oct 13, 2021

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants