Skip to content

Instantly share code, notes, and snippets.

@pythoninthegrass
Last active May 11, 2017 16:46
Show Gist options
  • Save pythoninthegrass/91dfe8e6cafcc0a6984763a4b4298c18 to your computer and use it in GitHub Desktop.
Save pythoninthegrass/91dfe8e6cafcc0a6984763a4b4298c18 to your computer and use it in GitHub Desktop.
create user on mac os x with dscl command
#!/usr/bin/env bash
# SOURCES:
# https://gist.github.com/rubytester/7330557
# http://stackoverflow.com/a/1923893
# Exit upon failed command
# set -e
# Logs
logTime=$(date +%Y-%m-%d:%H:%M:%S)
resetLog="/tmp/pkg_install_$logTime.log"
exec &> >(tee -a "$resetLog")
# Current user
loggedInUser=$(ls -l /dev/console | cut -d " " -f 4)
# Working directory
scriptDir=$(cd "$(dirname "$0")" && pwd)
# Check for root privileges
if [ "$(whoami)" != "root" ]; then
echo "Sorry, you need super user privileges to run this script."
exit 1
fi
# INPUT
# uname="tmuxpair"
# Accept user input while masking what is entered
echo "Enter Your Username:"
read uname
unset password
prompt="Enter Password:"
while IFS= read -p "$prompt" -r -s -n 1 char
do
if [[ $char == $'\0' ]]
then
break
fi
prompt='*'
password+="$char"
done
# COMPUTE
latestID=$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1)
userID=$((latestID+1))
# CREATE with sudo
dscl . -create /Users/$uname
dscl . -create /Users/$uname UserShell /bin/bash
dscl . -create /Users/$uname RealName $uname
dscl . -create /Users/$uname UniqueID "$userID"
dscl . -create /Users/$uname PrimaryGroupID 20 # admin group
dscl . -create /Users/$uname NFSHomeDirectory /Users/$uname
dscl . -passwd /Users/$uname $password
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment