编译环境:Centos6.4
要求:
创建账号不能使用useradd 可以使用password 一次性创建user10-------user20
[root@host2 ~]# vim 1.sh
1 #!/bin/bash
2 read -p "please input first account" FIRSTUSER
3 read -p "please input lastaccount" LASTUSER
4 FIRST=${FIRSTUSER##*[[:alpha:]]}
5 LAST=${LASTUSER##*[[:alpha:]]}
6 NAME=${LASTUSER%%[0-9]*}
7 #find current max uid and max gid
8 MAXUID=`cut -d: -f 3 /etc/passwd |sort -n |tail -n 1`
9 MAXGID=`cut -d: -f 4 /etc/passwd |sort -n |tail -n 1`
10 #create user account,modify /etc/passwd,/etc/shadow,/etc/group
11 for I in $(seq $FIRST $LAST);do
12 echo
13 "$NAME$I:x:$[$MAXUID+1]:$[$MAXGID+1]::/home/$NAME$I:/bin/bash">>/etc/
14 passwd
15 chmod 600 /etc/shadow
16 echo "$NAME$ID::::::::">>/etc/shadow
17 echo "$NAME$ID:x:$[$NAME$ID+1]:">>/etc/group
18 #create home dir and mail file
19 mkdir /home/$NAME$I
20 cp -r /etc/skel/. /home/$NAME$I
21 chown -R $NAME$I:$NAME$I /home/$NAME$I
22 touch /var/spool/mail/$NAME$I
23 chown $NAME$I:mail /var/spool/mail/$NAME$I
24 #create user password
25 echo "123"|passwd --stdin $NAME$I >/dev/null
26 let MAXUID=$MAXUID+1
27 let MAXGID=$MAXGID+1
28 done
运行脚本1.sh
[root@host2 ~]# bash 1.sh
please input first account u10
please input last account u20
查看所创建的用户
[root@host~]# tail /etc/passwd
u10:x:507:508::/home/u10:/bin/bash
u11:x:508:509::/home/u11:/bin/bash
u12:x:509:510::/home/u12:/bin/bash
u13:x:510:511::/home/u13:/bin/bash
u14:x:511:512::/home/u14:/bin/bash
u15:x:512:513::/home/u15:/bin/bash
u16:x:513:514::/home/u16:/bin/bash
u17:x:514:515::/home/u17:/bin/bash
u18:x:515:516::/home/u18:/bin/bash
u19:x:516:517::/home/u19:/bin/bash
u20:x:517:518::/home/u20:/bin/bash