DirectNET

Data Center Management Solutions including UPS Systems, Data Center Cooling, KVM over IP & IP Power Strips, Server Racks and Server Rack accessories; KVM Switches and KVM Extenders; Rackmount Monitors and Rackmount Keyboards.


NAVIGATION
Home
Store
INSIDE MAC
Television Shows
Broadcast Shows
Daily News Shows
Special Shows
EVENTS
DAILY TIPS
Design
Mac OS X
Mac OS X UNIX
COMMUNITY
Surveys
NEWS
Current
Press
Archive
FEATURES
Editorial
Dr. Mac
Reviews
Reader Reports
RESOURCES
FAQ
Documentation
Learning Center
MAN pages
Glossary
Tutorials
Tips
Links

OUR PARTNERS

OSXFAQ Mac OS X UNIX Tip-of-the-Day   back to index

Week 91 - User Accounts (6 December 2004)

by Adrian Mayo - Editor, OSXFAQ

Friday - Add Users to a Group

This script adds one or many users to a group. Give the group names and a list of user short names.

Lots of checks are made, as you can see from the script comments.

Grab the script from here.

NOTE: THIS SCRIPT IS WRITTEN FOR PANTHER (10.3)

#!/bin/bash

# Add new users to a group.
# Adds a user (or many users) to an existing group in NetInfo

usage () { echo "Add a user (or several users) to an existing group" echo "Usage: ${0##*/} group user [user...]" if [ "$*" != "" ]; then echo " Error: $*"; fi exit 1 }

# Ensure user is root # if [ "$USER" != "root" ]; then echo "Must be run as root." exit 1 fi

# Check parameters # if [ $# -lt 2 ]; then usage fi
group=$1
# search NetInfo for the given group - it should exist str="$(nireport . /groups name | grep -w $group)" if [ -z "$str" ]; then usage "Group $group does not exist" fi
# get the group number from the name gid="$(nireport . /groups gid name | grep $group | cut -f 1)"
# Drop the group and loop thro' additional parameters (users) to add to group # shift
for user in $*; do # check if the user exists struser="$(nireport . /users name | grep -w $user)" # check if the user already belongs to the group stringroup=$(nireport . /groups name users | grep -w "$group[[:space:]].*$user") # check if this is the user's primary group strprimary=$(nireport . /users name gid | grep -w "$user[[:space:]].*$gid")
#echo "user $struser, ingroup $stringroup, primary $strprimary"
# ensure that the user exists... if [ -z "$struser" ]; then echo "User $user does not exist" # ...and does not already belong to the group... elif [ ! -z "$stringroup" ]; then echo "User $user already belongs to group $group - not added again" # ...and this is not the user's primary group elif [ ! -z "$strprimary" ]; then echo "This is the user's primary group - not added" else # add user to the group dscl . merge /groups/$group users "$user" echo "$user added to group $group" fi done
exit 0

Discuss this trick in the Learning Center forum


If you want to learn more about Mac OS X Unix visit the Learning Center  click.

  • For beginners: Mac OS X Unix Tutorials
  • For detailed information on specific topics: Advanced Unix
  • For OS X in gereral: Mac OS X Tutorials

Copyright © 2000-2010 Inside Mac Media, Inc. All rights reserved.
Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users.
Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc.