题目:用shell脚本批量创建用户stu01-stu10,并生成8个字符的随机密码
一、解题思路:
1、在linux系统下添加用户命令--useradd
useradd stu01
2、非交互式给用户设置密码
echo "123456" | passwd --stdin stu01
3、数字前加0
{01..10}
3、随机生成8位字符串
echo $RANDOM | md5sum | cut -c 1-8
4、循环的思路添加用户
for ;do ;done
二、开发shell脚本:#!/bin/bash
#Desctiption: Create 10 users in batches and randomly generate 8-character passwords
#加载系统函数库,用到action函数
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
#判断是否当前用户是否为root用户
if [ $UID -ne 0 ];then
echo "Pls run this script by root"
exit 1
fi
#批量创建用户
for user in stu{01..10}
do
#判断要创建的用户是否已经存在。如果存在,给出错误提示,继续下一次循环
num=$(grep -w "$user" /etc/passwd | wc -l)
if [ $num -ne 0 ];then
<