-1

我想制作一个脚本,它检查 OS X 版本并根据调用的 sysadminctl 或 dscl。如您所见,该脚本正在检查操作系统版本并检查下一个可用的用户 ID。之后,脚本跳转到末尾并给出文件意外结束的错误。这里缺少什么?

谢谢你的帮助!

#!/bin/sh

#  create_user_1.2.sh
#  
#
#

if [ "$(id -u)" != "0" ]; then
echo "Sorry, you are not root."
exit 1
fi


# === For creating a User we need some input! ===

echo "Enter your desired user name: "
read USERNAME

echo "Enter a full name for this user: "
read FULLNAME

echo "Enter a password for this user: "
read -s PASSWORD

# ====


# A list of (secondary) groups the user should belong to
# This makes the difference between admin and non-admin users.

echo "Is this an administrative user? (y/n)"
read GROUP_ADD

if [ "$GROUP_ADD" = n ] ; then
    SECONDARY_GROUPS="staff"  # for a non-admin user
elif [ "$GROUP_ADD" = y ] ; then
    SECONDARY_GROUPS="admin _lpadmin _appserveradm _appserverusr" # for an admin user
else
    echo "You did not make a valid selection!"
fi

# ====

# check the OS X Version
OSXVERSION=$(sw_vers -productVersion | awk -F '.' '{print $1 "." $2}')

# Create a UID that is not currently in use
echo "Creating an unused UID for new user..."

# Find out the next available user ID
MAXID=$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1)
USERID=$((MAXID+1))

#if osx 10.10 then run
if [[ "$OSXVERSION" == "10.11" ]]; then
sysadminctl -addUser $USERNAME -fullName "$FULLNAME" -UID=$USERID -password $PASSWORD

else


#if osx 10.10 then run
if [[ "$OSXVERSION" == "10.10" ]]; then
sysadminctl -addUser $USERNAME -fullName "$FULLNAME" -UID=$USERID -password $PASSWORD

else

#if osx 10.9 then run
if [[ "$OSXVERSION" == "10.9" ]]; then

# Create the user account by running dscl (normally you would have to do each of these commands one
# by one in an obnoxious and time consuming way.
echo "Creating necessary files..."

dscl . -create /Users/$USERNAME
dscl . -create /Users/$USERNAME UserShell /bin/bash
dscl . -create /Users/$USERNAME RealName "$FULLNAME"
dscl . -create /Users/$USERNAME UniqueID "$USERID"
dscl . -create /Users/$USERNAME PrimaryGroupID 20
dscl . -create /Users/$USERNAME NFSHomeDirectory /Users/$USERNAME
dscl . -passwd /Users/$USERNAME $PASSWORD

# Create the home directory
echo "Creating home directory..."
createhomedir -c 2>&1 | grep -v "shell-init"

fi

# Add user to any specified groups
echo "Adding user to specified groups..."

for GROUP in $SECONDARY_GROUPS ; do
dseditgroup -o edit -t user -a $USERNAME $GROUP
done

echo "Created user #$USERID: $USERNAME ($FULLNAME)"

exit 0
4

2 回答 2

0

答案很简单:'if' 'else' 需要与 'elsif' 保持正确的顺序:

#if osx 10.10 then run
if [[ "$OSXVERSION" == "10.11" ]]; then
    echo "OS is 10.11"
sysadminctl -addUser $USERNAME -fullName "$FULLNAME" -UID=$USERID -password $PASSWORD

#if osx 10.10 then run

elif [[ "$OSXVERSION" == "10.10" ]]; then
    echo "OS is 10.10"
sysadminctl -addUser $USERNAME -fullName "$FULLNAME" -UID=$USERID -password $PASSWORD

#if osx 10.9 then run

elif [[ "$OSXVERSION" == "10.9" ]]; then

# Create the user account by running dscl
echo "Creating necessary files..."

dscl . -create /Users/$USERNAME
dscl . -create /Users/$USERNAME UserShell /bin/bash
dscl . -create /Users/$USERNAME RealName "$FULLNAME"
dscl . -create /Users/$USERNAME UniqueID "$USERID"
dscl . -create /Users/$USERNAME PrimaryGroupID 20
dscl . -create /Users/$USERNAME NFSHomeDirectory /Users/$USERNAME
dscl . -passwd /Users/$USERNAME $PASSWORD

# Create the home directory
echo "Creating home directory..."
createhomedir -c 2>&1 | grep -v "shell-init"

fi
于 2015-11-02T13:58:03.457 回答
0

如果您在这里并且您的系统正在运行 10.10 和更高版本的任何内容,那么 sysadminctl 命令是您最好的朋友。它做了很多 DSCL 做不到的魔法。

这是 sysadminctl 的输出:

sysadminctl[21233:29122637] Usage: sysadminctl
    -deleteUser <user name> [-secure || -keepHome]
    -newPassword <new password> -oldPassword <old password> [-passwordHint <password hint>]
    -resetPasswordFor <local user name> -newPassword <new password> [-passwordHint <password hint>]
    -addUser <user name> [-fullName <full name>] [-UID <user ID>] [-password <user password>] [-hint <user hint>] [-home <full path to home>] [-admin] [-picture <full path to user image>]

Pass '-' instead of password in commands above to request prompt.

然后你会想做:

sudo createhomedir -c 2>&1 | grep -v "shell-init"

要添加/删除用户,请使用 dseditgroup:

sudo dseditgroup -o edit -a usernametoadd -t user admin
sudo dseditgroup -o edit -a usernametoadd -t user wheel
于 2017-07-24T12:09:19.037 回答