用脚本实现用户的创建与密码设定

题目要求:


编写 script.sh
(1) 用户名在文件/mnt/userfile
    密码在文件 /mnt/passfile
(2) 当要建立i的用户存在,不做任何操作
(3) 当脚本后指定文件个数少于2个,报错
    please give me userfile or passwd file
(4) 当所给文件的行数不一致
    报错


脚本: 
#!/bin/bash
while [ " $# " -lt " 2 " ]
do
echo "please give me userfile and passwdfile!"
break
done
while [ "`wc -l $1 &>/mnt/null;cut -d ' ' -f 1 /mnt/null`" != `wc -l $2 &>/mnt/null;cut -d ' ' -f 1 /mnt/null` ]
do
        echo "The userfile'line is different from passwdfile'line!"
        break
done


for ((MUN=1;MUN <= `wc -l $1 | awk '{print $1}'`;MUN++))
do
NAME=`sed -n ${MUN}p $1`
PASSWD=`sed -n ${MUN}p $2`
id $NAME &>/dev/null
if  [ "$?" -ne 0 ]
then
useradd $NAME
echo $PASSWD |passwd --stdin $NAME &>/dev/null
fi
done


脚本截图:
用脚本实现用户的创建与密码设定

测试:
[[email protected] ~]# vim /mnt/userfile
[[email protected] ~]# cat /mnt/userfile
user7
user8
[[email protected] ~]# vim /mnt/passfile 
[[email protected] ~]# cat /mnt/passfile 
987
654
321
[[email protected] ~]# id user7
uid=1008(user7) gid=1008(user7) groups=1008(user7)
[[email protected] ~]# id user 8
id: extra operand ‘8’
Try 'id --help' for more information.
[[email protected] ~]# id user8
id: user8: no such user
[[email protected] ~]# /mnt/script.sh /mnt/userfile /mnt/passfile 
The userfile'line is different from passwdfile'line!
[[email protected] ~]# id user8
uid=1009(user8) gid=1009(user8) groups=1009(user8)