-
-
Save Chuckytuh/3c48269361cbe51f198448714e80b223 to your computer and use it in GitHub Desktop.
Create User Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script creates a user account under Mac OS X | |
usage() { | |
echo " Synopsis" | |
echo ' create-user userName userFullName userPassword groupId' | |
echo "" | |
echo "" | |
echo " Description" | |
echo " Creates a new user with the given params and attributes to the desired group" | |
} | |
UName=$1 | |
FullName=$2 | |
Password=$3 | |
GroupId=$4 | |
if [[ $UID -ne 0 ]]; then echo "Please run $0 as root." && exit 1; fi | |
if [ $# -lt 4 ]; then | |
echo "Wrong arguments supplied!" | |
usage | |
exit 2 | |
fi | |
# Find out the next available user ID | |
MAXID=$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1) | |
UserId=$((MAXID+1)) | |
# Create the user account | |
echo "Creating user $UName" | |
dscl . -create /Users/"$UName" | |
echo "Setting default shell to /bin/bash" | |
dscl . -create /Users/"$UName" UserShell /bin/bash | |
echo "Setting RealName as $FullName" | |
dscl . -create /Users/"$UName" RealName "$FullName" | |
echo "Setting UniqueId as $UserId" | |
dscl . -create /Users/"$UName" UniqueID "$UserId" | |
echo "Setting PrimaryGroupID as $GroupId" | |
dscl . -create /Users/"$UName" PrimaryGroupID "$GroupId" | |
echo "Creating home directory /Users/$UName" | |
dscl . -create /Users/"$UName" NFSHomeDirectory /Users/"$UName" | |
createhomedir -c -u "$UName" | |
echo "Defining user password" | |
dscl . -passwd /Users/"$UName" "$Password" | |
if [ $? != 0 ] ; then | |
echo "Reverting user creation. Deleting user /Users/$UName" | |
dscl . delete /Users/"$UName" | |
exit 1 | |
fi | |
echo "" | |
echo "Created user #$UserId: $UName ($FullName)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment