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

有两个文件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 ~]# 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值