天天看點

腳本練習:建立使用者

腳本練習:

要求:

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,如需轉載請自行聯系原作者