用shell脚本创建20个用户,要求给20个用户指定家目录为/share/home/中,并且给每一个用户随机指定一个密码,最后把这些用户信息记录在日志里!
[root@client sbin]# vim 2.sh if [ -e /share/home ]; then mv /share/home /share/home.back mkdir -p /share/home else mkdir -p /share/home fi create_user(){ for i in `seq 1 3` ;do useradd -d /share/home/user$i user$i password=`mkpasswd -s 0 -d 1` echo $password |passwd --stdin user$i echo "user$i password is password" >> /tmp/log1 done } if rpm -qa |grep -q expect;then create_user else yum install -y expect create_user fi
验证一下:
[root@lianxi1 sbin]# cat /tmp/log user1 password is 3yequfcQH user2 password is qp4oYGqvs user3 password is pzJarE5xk [root@lianxi1 sbin]# tail -n3 /etc/passwd user1:x:500:500::/share/home/user1:/bin/bash user2:x:501:501::/share/home/user2:/bin/bash user3:x:502:502::/share/home/user3:/bin/bash [root@lianxi1 sbin]#
那个mkpqsswd工具是随机产生密码的,若系统中一般没自带这个工具,需要安装,他安装命令是yum install -y expect,这也是最后判断expect的原因所在!