Bash script to create Mac OS X user accounts

Edit this script to place the desired username and password information.

#!/bin/bash
#
#=======================================================================#
# Author: SITHnews.com
# Date: 2012.09.14
# Version: 1.0
#
# Version 1.0: Initial version
# Creates a user account with the specified parameters built in to the
# script. This is useful for predefining accounts on a Mac.
#=======================================================================
#===========================================
# Customizable variables:
#===========================================
#Each of these five variables are passed to a separate
#call of the createUserAccount function at the end of this script.
#These calls must occur after the function is defined.
newuser_realname=COAL
newuser_username=coal
newuser_password=
#true or false
newuser_admin=true
#optional - list groups with spaces
newuser_secondarygroups=""
#===========================================
# Begin rest of script:
#===========================================
#####################################
# Set the logging parameters
#####################################
currentDate=`date "+%m-%d-%Y--%H-%M.%S"`
scriptLog="/var/log/management.log"
#Log
#Send stdout to $scriptLog, and then stderr(2) to stdout(1)
exec 1>>$scriptLog 2>&1
#Show all commands in the log
#set -x
echo -e "n===================================================="
echo -e "Script: $0"
echo -e "Runtime: $currentDaten"
#####################################
# functions
#####################################
function verifyUserName {
local userNameLookup=$(sudo dscl . -list /Users | grep $1)
if [ "$userNameLookup" == "$1" ]; then
echo "Error: $1 already exists. Exiting..."
exit
else
echo "Selected username $1 is unique."
fi
} #end verifyUserName
function verifyUserID {
local newuser_userIDLookup=$(sudo dscl . -list /Users UniqueID | grep "$1" | awk ‘{print $2}’ )
if [ "$newuser_userIDLookup" == "$1" ]; then
echo "Error: User ID $1 already exists. Exiting..."
exit
else
echo "Selected user ID $1 is unique."
fi
} #end verifyUserID
function createUserAccount {
#get the passed in parameters
local createUserAccount_realname=$1
local createUserAccount_username=$2
local createUserAccount_password=$3
local createUserAccount_admin=$4
local createUserAccount_secondarygroups=$5
#clean up input
createUserAccount_username=`echo $createUserAccount_username | tr '[A-Z]' '[a-z]'`
createUserAccount_admin=`echo $createUserAccount_admin | tr '[A-Z]' '[a-z]'`
#call a function to verify the chosen username
verifyUserName $createUserAccount_username
#Determine the uid that can be applied
upperBound=$(sudo dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1)
createUserAccount_userID=$((upperBound+1))
#call the function to verify this ID
verifyUserID $createUserAccount_userID
#These commands create the user
sudo dscl . -create /Users/""$createUserAccount_username""
sudo dscl . -create /Users/""$createUserAccount_username"" UserShell /bin/bash
sudo dscl . -create /Users/""$createUserAccount_username"" RealName "$createUserAccount_realname"
sudo dscl . -create /Users/""$createUserAccount_username"" UniqueID $createUserAccount_userID
sudo dscl . -create /Users/""$createUserAccount_username"" PrimaryGroupID 20
sudo dscl . -create /Users/""$createUserAccount_username"" NFSHomeDirectory /Users/""$createUserAccount_username""
sudo dscl . -passwd /Users/""$createUserAccount_username"" $createUserAccount_password
if [ "$createUserAccount_admin" = "true" ] ; then
sudo dscl . -append /Groups/admin GroupMembership "$createUserAccount_username"
sudo dscl . -append /Groups/_appserveradm GroupMembership "$createUserAccount_username"
sudo dscl . -append /Groups/_appserverusr GroupMembership "$createUserAccount_username"
fi
for secondary_groups in $createUserAccount_secondarygroups ; do
dseditgroup -o edit -t user -a $createUserAccount_username $secondary_groups
done
echo "Added user with the following parameters:"
echo "Real name: $createUserAccount_realname"
echo "User name: $createUserAccount_username"
echo "Admin: $createUserAccount_admin"
echo "Secondary groups: $createUserAccount_secondarygroups"
exit
} #end function createUserAccount
#####################################
#call the function to create the account
#####################################
createUserAccount "$newuser_realname" "$newuser_username" "$newuser_password" "$newuser_admin" "$newuser_secondarygroups"

 
 

2 Comments

    • It’s what’s called a bash script – so you need to write it in a text editor, save it with a .sh extension and run it from Terminal. For example, if you named it “makenewuser.sh” you would open Terminal and do the following:
      chmod +x makenewuser.sh
      sudo ./makenewuser.sh

Leave a Reply

Your email address will not be published.


*