$3 versus ~ variable

daworley
Contributor II

Hey all,

I have this script that I cobbled together from a few sources. It's designed to clean up the Lion sidebar and remove items. I plan to scope it to run at login to clean up the Finder window.

My problem is getting the $3 variable to pull out the home folder path. Running as user is great, but I need to pass the home folder quality from that.

This script works great when running it manually from Terminal. I tried something more convoluted with Finger and grepping for Directory but can't get the sed/awk/cut whatever to work properly.

Anybody have a bright idea?
Thanks! - D

#!/bin/sh

home=~
pb=/usr/libexec/PlistBuddy plist="$home/Library/Preferences/com.apple.sidebarlists.plist"

${pb} -c "Add :networkbrowser dict" "${plist}" ${pb} -c "Add :networkbrowser:CustomListProperties dict" "${plist}" ${pb} -c "Add :networkbrowser:CustomListProperties:com.apple.NetworkBrowser.backToMyMacEnabled bool false" "${plist}" ${pb} -c "Set :networkbrowser:CustomListProperties:com.apple.NetworkBrowser.backToMyMacEnabled false" "${plist}"

${pb} -c "Delete :favorites:VolumesList:1 dict" "${plist}" ${pb} -c "Delete :favorites:VolumesList:0 dict" "${plist}" ${pb} -c "Delete :favorites:VolumesList:4 dict" "${plist}"

7 REPLIES 7

bentoms
Release Candidate Programs Tester

Try;

loggedInUser=/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'

loggedInUserHome=dscl . -read /Users/$loggedInUser | grep NFSHomeDirectory: | cut -c 19- | head -n 1

Regards,

Ben.

tlarkin
Honored Contributor

Parameters in scripts ran by Casper only get applied at log in, or via self service. So, if you want to use the built in $1, $2, $3 you need to run the script at login. Otherwise, you can use different detection methods:

ls -l /dev/console | awk '{ print $3}'

or the Jared Nichols way:

ls -l /dev/console | cut -d " " -f4

you can also use the stat command to grab the owner of /dev/console. Basically, testing for ownership of /dev/console is a pretty solid method and there are many ways to do it. Another option would be to not use script but rather deploy launch agents, which already execute code as the user.

-Tom

jarednichols
Honored Contributor

Ha, way to give me credit :)
---
Jared F. Nichols
Desktop Engineer, Client Services
Information Services Department
MIT Lincoln Laboratory
244 Wood Street
Lexington, Massachusetts 02420
781.981.5436

tlarkin
Honored Contributor

Not gonna steal your thunder buddy :-)

Not applicable

Off on a tangent somewhat, I'm curious what items you are and are not
On 15 December 2011 05:47, Douglas <douglasworley at mac.com> wrote:
providing in the Finder sidebar?

Cheers,
Doug

sean
Valued Contributor

Personally prefer the stat method

stat -f%Su /dev/console

No piping required, but I don't think this was his question. If I'm reading your email correctly what you were after was the users home directory. For a login script, using the $3 variable:

su $3 -c 'printenv HOME'

N.B. This could throw up an error if the current pwd is, for example, roots home directory
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied

But it will still provide the correct answer. Easy to suppress the error though,

su $3 -c 'printenv HOME' 2>1

Alternatively, if you would rather not suppress errors, then you could do either:

su $3 -c 'cd ~/; pwd'
or
su $3 -c 'cd ~/; printenv HOME'

All will give the correct answer and better than trying to interrogate directory services, particularly if you are working in a mixed environment of network and local users.

As an aside, you could add this to your scripts:

currentUser=$3

if [[ $3 == "" ]]
then currentUser=stat -f%Su /dev/console
fi

This way, your script will always provide the correct answer, but will only bother to run the extra command if required. Alternatively, don't bother with the $3. It's a shame you can't use $3 when using the script other than at login/logout or Self Service.

Sean

jarednichols
Honored Contributor

Airdrop. Disabled it.

j
---
Jared F. Nichols
Desktop Engineer, Client Services
Information Services Department
MIT Lincoln Laboratory
244 Wood Street
Lexington, Massachusetts 02420
781.981.5436