批量添加用户

[root@server0 ~]# ll
total 24
-rw-------. 1 root root 8619 May  7  2014 anaconda-ks.cfg
-rwxr-xr-x. 1 root root  336 Dec 24 00:11 batchusers
drwxr-xr-x. 2 root root    6 Dec 16 21:06 Desktop
drwxr-xr-x. 2 root root    6 Dec 16 21:06 Documents
drwxr-xr-x. 2 root root    6 Dec 16 21:06 Downloads
-rw-r--r--. 1 root root 3785 Feb 17  2017 mariadb.dump
drwxr-xr-x. 2 root root    6 Dec 16 21:06 Music
drwxr-xr-x. 2 root root    6 Dec 16 21:06 Pictures
drwxr-xr-x. 2 root root    6 Dec 16 21:06 Public
drwxr-xr-x. 2 root root    6 Dec 16 21:06 Templates
-rw-r--r--. 1 root root   21 Dec 24 00:00 user1.txt
drwxr-xr-x. 2 root root    6 Dec 16 21:06 Videos
[root@server0 ~]# ./batchusers 
Usage: /root/batchusers userfile
[root@server0 ~]# ./batchusers  /etc/
Input file not found
[root@server0 ~]# ./batchusers  user1.txt 
user yange1 is  created
user yange2 is  created
user yange3 is  created
[root@server0 ~]# cat batchusers 
#!/usr/bin/bash
if [ $# -eq 0 ];then
    echo "Usage: /root/batchusers userfile"
    exit 1
fi
if [ ! -f "$1"  ];then
    echo "Input file not found"
    exit 2 
fi

for user in `cat $1`
do
    id $user &>/dev/null
    if [ $? -eq 0 ];then
        echo "user $user already exists"
    else
        useradd $user -s /bin/false
        echo "user $user is  created"
    
    fi
done
[root@server0 ~]#
原文地址:https://www.cnblogs.com/liweiming/p/10164639.html