I have the following script for creating a new user on macOS:
#!/bin/bash
export PATH=/usr/bin:/bin:/usr/sbin:/sbin
consoleUser() {
echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ && ! /loginwindow/ { print $3 }'
}
displayfortext() { # $1: message $2: default text
message=${1:-"Message"}
defaultvalue=${2:-"default value"}
user=$(consoleUser)
if [[ $user != "" ]]; then
uid=$(id -u "$user")
launchctl asuser $uid /usr/bin/osascript <<-EndOfScript
text returned of ¬
(display dialog "$message" ¬
default answer "$defaultvalue" ¬
buttons {"OK"} ¬
default button "OK")
EndOfScript
else
exit 1
fi
}
realname=$(displayfortext "Enter the real Name (e.g. John Doe)" "John Doe")
username=$(displayfortext "Enter the Username (e.g. john.doe)" "john.doe")
sudo dscl . -create /Users/$username RealName $realname #Set Real Name (e.g. John Doe)
Now after the last sudo command (and a few others I left out) the user is successfully created. The problem is, that the users name (John Doe) is only "John" and everything behind the whitespace disappeared.
Does someone have an idea how to fix this? I do not want another character between John and Doe (nothing like John_Doe) it has to be a whitespace (company policy).