PackageMaker v the home directory

So, it turns out that if you want to use a shell script in PackageMaker (say the post-installation ‘postflight’ script file), if you try referring to the user’s home directory using ‘~’ you wind up referring to a directory at the root level (ie /~/blah), which is invariably what you don’t want. The solution referred to on the Apple discussion boards is to create a shell script that moves everything to the home directory then execute that from the install script.

So we start by getting the username. But of course, the postflight script is running under sudo so it’s username probably isn’t the same as the logged in user.

>sudo id -p
Password:
login   testuser
uid     root
groups  wheel daemon kmem sys tty certusers operator admin staff

Looks like the ‘login’ line is the one we want, and we can get it using grep.

>sudo id -p | grep login
Password:
login        testuser

But we need the second column (ie just “testuser” rather than “login testuser”). awk lets us get at the second column using

>sudo id -p | grep login | awk '{print $2}'
testuser

In the end you could try something along the lines of the following in your PackageMaker postflight script. Note that the backticks “ evaluate the expression contained within them.

set username="blah" # otherwise bash seems to whinge that username is undefined on the next line
username="`sudo id -p | grep login | awk '{print $2}'`"
mv whatever /Users/$username/wherever/

This seems to work as long as you’re not doing the install via Apple Remote Desktop’s Install Package option. Because if you do that the user is root and the package probably can’t be meaningfully installed in root’s home directory. The only solution I could come up with is to install your files in a location that’s commonly accessible to all users.

Leave a Reply

Your email address will not be published. Required fields are marked *