老男孩出的Shell编程企业面试题4:
批量创建10个系统帐号oldboy01-oldboy10并设置密码(密码为随机8位字符串)。
我的Shell脚本如下:
#!/bin/bash for num in 01 02 03 04 05 06 07 08 09 10 do username=oldboy$num password=`head -c 500 /dev/urandom | md5sum | head -c 8` egrep "^$username" /etc/passwd >& /dev/null if [ $? -ne 0 ] then useradd $username echo $password | passwd $username --stdin &>/dev/null if [ $? -eq 0 ] then echo "$username:$password" fi fi done
运行结果如下:
[root@CentOS66 shellstudy]# ./creatuser.sh oldboy01:d7e86b4e3b oldboy02:7fbc6033fc oldboy03:aabd217934 oldboy04:bbe4b5a094 oldboy05:38041b6159 oldboy06:e51d10d669 oldboy07:c292390561 oldboy08:61badd5747 oldboy09:60eed14fda oldboy10:a5d5a6c1f6