有两个文件userfile及passwdfile,分别存有用户名和密码,编写一个脚本,可以自动识别userfile里的用户名,然后建立此用户名,并且匹配passwdfile里的密码
文件内容如下
[root@localhost ~]# cat userfile
user1
user2
user3
[root@localhost ~]# cat passwdfile
123
123
123
[root@localhost ~]# vim lianxi.sh
下边是脚本内容
#!/bin/bash
MAX_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 --stdin
done
测试结果:
[root@localhost ~]# sh lianxi.sh userfile
Changing 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 user1
uid=1001(user1) gid=1001(user1) groups=1001(user1)
[root@localhost ~]# id user2
uid=1002(user2) gid=1002(user2) groups=1002(user2)
[root@localhost ~]# id user3
uid=1003(user3) gid=1003(user3) groups=1003(user3)
[root@localhost ~]#