Shell脚本_创建系统用户,从一个用户列表文件(6.18重构)。

#!/bin/bash
#This shell script is used to add user from a list of files.
#So, it must have a file name as a parameter.
#If you specify group name, this shell script to add all users to the group.
#Usage: ThisSH filename [groupname]
#Author: Mach
#Modify time: 12.11/2015,create
#Modify time: 06.18/2016,add: determine the user identity, refactoring

function Adduser(){
    for NM in $(cat $1)
    do
        useradd $NM && echo "Add user $NM successful."
        if [ ! -z $2 ]; then
            usermod -G $2 $NM && echo "Add user $NM to group $2 successful."
        fi
        echo -e "user:$NM\t\tgroup:$(id -Gn $NM)" >> ./newadduserlist.log
        echo $RANDOM | tee -a ./newadduserlist.log | passwd --stdin $NM
        echo -e "\n" >> ./newadduserlist.log
    done
}

if [ ! $UID = 0 ]; then
    echo "Error: Only root can add users."
    exit 1
elif [ $# = 0 ]; then
    echo "Error: No parameters."
    exit 2
elif [ ! -f $1 ]; then
    echo "Error: File $1 non-existent."
    exit 3
fi

case $# in
    1)
        Adduser $1
        ;;
    2)
        grep -w "^$2" /etc/group &> /dev/null
        if [ ! $? = 0 ]; then
            useradd -Ms /sbin/nologin $2 && echo "Add UNIgroup $2 successful."
            echo "$(id -un $2) (unified ID group)" >> ./newadduserlist.log
            echo -e "\n" >> ./newadduserlist.log
        fi
        Adduser $1 $2
        ;;
    *)
        echo "Error: Parameters error."
        ;;
esac
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值