Shell script to create local homes

Hi
I am trying to write a login script for network based clients (OSX) that looks up local accounts eg admin, root, etc and exits the script so that it doesn't apply to them. Then for everyone else I make folders eg movies, music, etc that are placed in local harddrive rather than the default place on the server harddrive.
At the moment this script creates the desired effect for the root user no matter who logs in. If I remove the first couple of lines that try to sort out the admin users it works for everyone who logs in. However I want it to work for everyone except root and local users.
ie
#!/bin/sh
#
CURRUSER=`whoami`

ADMINGROUP=`groups | grep admin`
if [ `expr "$ADMINGROUP" != ""` = 1 ]
then
exit 1
fi

mkdir -p /Users/Shared/$CURRUSER/Movies
mkdir -p /Users/Shared/$CURRUSER/Music
mkdir -p /Users/Shared/$CURRUSER/Pictures
mkdir -p /Users/Shared/$CURRUSER/Music/iTunes
mkdir -p /Users/Shared/$CURRUSER/Music/GarageBand
mkdir -p /Users/Shared/$CURRUSER/Pictures/iPhoto Library

rm -R ~/Movies
rm -R ~/Music
rm -R ~/Pictures

ln -s /Users/Shared/$CURRUSER/Movies ~/Movies
ln -s /Users/Shared/$CURRUSER/Music ~/Music
ln -s /Users/Shared/$CURRUSER/Pictures ~/Pictures

Hope someone can help.
Ta

I couldn't get your if statement to work (but that could be due to the OS I'm running - can't tell since you didn't post yours).

Try this instead:

ADMINGROUP=`groups | egrep -c admin`

if [ "$ADMINGROUP" > 0 ]
   then
      exit 1
fi

OSX 10.3

Thanks I'll give it a try.

Hi I tried it and it makes sense but the script does not quite work yet , a bit of progress has been made at this link.

http://discussions.info.apple.com/webx?13@21.Gbrha3sRzeY.1@.599cb736.689d315b/3

My script currently looks like this

#!/bin/sh
#
CURRUSER=`whoami`

ADMINGROUP=`groups | egrep -c admin`

if [ "$ADMINGROUP" > 0 ]
then
exit 1
fi

mkdir -p /Users/Shared/$CURRUSER
mkdir -p /Users/Shared/$CURRUSER/Movies
mkdir -p /Users/Shared/$CURRUSER/Music
mkdir -p /Users/Shared/$CURRUSER/Music/iTunes
mkdir -p /Users/Shared/$CURRUSER/Music/GarageBand

rm -R ~/Movies
rm -R ~/Music

ln -s /Users/Shared/$CURRUSER/Movies ~/Movies
ln -s /Users/Shared/$CURRUSER/Music ~/Music

I will add the chown part but it still needs something to create the $CURRUSER directories rather than roots . This last sentence makes sense if you read the input at the link above.

Add logic such as a prompt to actually set the value of CURRUSER to the ID of the person who will own the directories. Since you are running as root, then you could issue the command su $CURRUSER without the need to supply a password.

Would I do that just before I make the directories in the script and I am not quite sure how.
I guess I don't understand how your trying to use su.
Could you give an example in my script how you might do it.
Thanks

#I had thought for some reason that the script was being
#executed by root and not the everyday user

Are you saying it works for root but not others? Are you getting any permission errors, unable to create directory errors?

How are you executing the script? Does root own the script and do you have the SUID permission bit set? This would allow anyone who executes the script to run the script as if they were root (security issues with SUID's). What permissions are set for this script?

I use a login window manager from (its free)http://www.bombich.com/mactips/loginhooks.html
and this uses unix to login hook (LoginHook (POSIX path)) and it allows you to store a script on the hardrive or server and executes it at login. It runs the script with root permissions ( I assume) otherwise I don't think it would be able to create directories and sub directories. Theres good info on this page (link above) eg
Create a shell script to be run upon login. The name of the script is not important, as long as it is the same as what you will indicate in step 4. Note that the name of the user logging in is sent as an argument ($1) to this script. You can also download this basic script and copy it to /Library/Management (you may have to create this folder). Be sure to check out my Scripts Library for several other scripts that perform various tasks.

All my client machines are Mac osx 10.35 with an Admin and Root local users. Everyone else who logs in are not local users but are located on an Xserve LDAP binded database.

The reason I am trying to create certain directories locally for users as they login rather than have them on the server is because we run a wireless network that can't cope with huge movie and music files.

I hope this clears things a little.

Thanks

Do you think I need to change the $CURRUSER to $1

Yes, give that a try. Note that for this to work, the script will be executed as follows:

your_script_name.sh user_name

Inside of your script, change CURRUSER=`whoami` to CURRUSER=$1

After changing the script so that CURRUSER=$1 the script creates the directories for all users that login.

The 2 things it still does not do is:

  1. Exit the script when local admin users login.

  2. Remove the network Movie and Music directories and create virtual linked directories to the ones on the local drive (users/shared/..).

It seems we are starting to get close.

Any suggestions to try next.

This may be the way to go

Problems are probably due to the script running as root which means ~ still resolves to root's home.

What you should probably do is change CURRUSER $(logname) or even just $USER for that matter.

create a new shell script:

#!/bin/sh

sudo -u $1 -H <pathToOriginalScript>

exit 0

Make this one your LoginHook.

This will run your script as the user logging instead of running it as root, and set the ~ variable to the target users $HOME.

You may also want to use 'id -Gn' instead of 'groups' as 'groups' has been made obsolete by 'id'.