练习:编写一个脚本,可以自动建立用户,及匹配密码

来源:互联网 发布:淘宝买手店 编辑:程序博客网 时间:2024/05/29 08:18

有两个文件userfile及passwdfile,分别存有用户名和密码,编写一个脚本,可以自动识别userfile里的用户名,然后建立此用户名,并且匹配passwdfile里的密码

文件内容如下

[root@localhost ~]# cat userfile user1user2user3[root@localhost ~]# cat passwdfile 123123123
[root@localhost ~]# vim lianxi.sh下边是脚本内容#!/bin/bashMAX_LINE=`wc -l $1 | awk -F " " '{printf $1}'`for NUM in `seq 1 $MAX_LINE`do   USERNAME=`sed -n ${NUM}p $1`   useradd $USERNAME > /dev/null   echo $(sed -n ${NUM}p passwdfile) | passwd $USERNAME --stdindone                                         
测试结果:[root@localhost ~]# sh lianxi.sh userfileChanging password for user user1.passwd: all authentication tokens updated successfully.Changing password for user user2.passwd: all authentication tokens updated successfully.Changing password for user user3.passwd: all authentication tokens updated successfully.[root@localhost ~]# id user1uid=1001(user1) gid=1001(user1) groups=1001(user1)[root@localhost ~]# id user2uid=1002(user2) gid=1002(user2) groups=1002(user2)[root@localhost ~]# id user3uid=1003(user3) gid=1003(user3) groups=1003(user3)[root@localhost ~]# 
0 0