如何在linux / bash脚本中将预定义用户添加到特定组

时间:2015-12-12 14:16:16

标签: shell

我一直在尝试实现一个代码,该代码可以创建预定义的用户,放入特定的组( MyMembers 中的前5个, MyGroup 中的下5个,以及最后5到 MyMinions ),但我总是迷失在编码中。

到目前为止,这是我在创建预定义用户中的代码。

#!/bin/bash
#This script adds a user with a hidden password to your #system.

ans=yes
while [[ "$ans" = yes ]] ;
do

if [ $(id -u) -eq 0 ];
then
    read -p "Enter username: " username
    read -s -p "Enter password: " password
    egrep "^$username" /etc/passwd >/dev/null

if [ $? -eq 0 ];
then
    echo "$username already exists!"
    exit 1
else
    pass=$(perl -e 'print crypt ($ARGV[0], "password")' $password)
    useradd -m -p $pass $username
    [ $? -eq 0 ] && echo -e "\nUser has been added to your system!" || echo "\nFailed to add the user!"
fi
else
    echo "Only root may add a user to the system"
    exit 2
fi

echo -e "\nDo you still want to add more users?. \nType yes to continue adding. \nType yes or any key to exit"

read ans
done
exit

0 个答案:

没有答案