天天看点

脚本练习:建立用户

脚本练习:

要求:

1 编写的脚本script.sh /mnt/userfile /mnt/password

2 当要建立的用户已经存在不做任何操作

3 当脚本所指定文件个数少于两个显示:

pleasegive  me userfile  or  password file

4 当所给额文件行数不一致时显示:

/mnt/userfile‘s  line  is  different  from  /mnt/password's line

脚本如下:

#!/bin/bash

while [ "$#" -lt "2" ]

do

echo please give me userfle or password file

exit 1

done

COUNT=`wc -l $1 | cut -d " " -f 1`

COUNT1=`wc -l $2 | cut -d " " -f 1`

while [ $COUNT -ne $COUNT1 ]

echo "/mnt/userfile's line is different from /mnt/password's line"

NUM=1

COUNT2=$[$COUNT+1]

while [ "$NUM" -lt "$COUNT2" ]

USERNAME=`sed -n ${NUM}p $1`

PASSWD=`sed -n ${NUM}p $2`

id $USERNAME &> /dev/null

while [ "$?" -ne "0" ]

useradd $USERNAME &> /dev/null

echo $PASSWD | passwd --stdin $USERNAME &> /dev/nul

NUM=$[$NUM+1]

测试:

[root@localhost ~]# cat /mnt/userfile

westos

westos1

westos2

westos3

westos4

[root@localhost ~]# cat /mnt/password

123

234

567

896

[root@localhost ~]# script.sh /mnt/userfile /mnt/password

[root@localhost ~]# id westos

uid=1008(westos) gid=1008(westos) groups=1008(westos)

[root@localhost ~]# id westos1

uid=1009(westos1) gid=1009(westos1) groups=1009(westos1)

[root@localhost ~]# id westos2

uid=1010(westos2) gid=1010(westos2) groups=1010(westos2)

[root@localhost ~]# id westos3

uid=1011(westos3) gid=1011(westos3) groups=1011(westos3)

[root@localhost ~]# id westos4

uid=1012(westos4) gid=1012(westos4) groups=1012(westos4)

[root@localhost ~]# vim /mnt/password

/mnt/userfile's line is different from /mnt/password's line

[root@localhost ~]# script.sh /mnt/userfile

please give me userfle or password file

[root@localhost ~]# script.sh

本文转自blueclo51CTO博客,原文链接: http://blog.51cto.com/12774272/1939128,如需转载请自行联系原作者