#!/bin/sh

function getpassword
{

##产生随机密码
if [ -n "$1" ]
then length=$1
else
length=8

fi

chars='abcdefghijkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789'

charlength=echo $chars |wc -m |sed 's#^[ \t]##g'

pw=''

for nn in seq ${length}
do

r=$[$RANDOM%$charlength+1]

w=echo $chars |cut -c $r-$[$r]
pw=$pw$w
done
echo $pw
}

[ -f name_password.txt ] && rm -fr name_password.txt

for name in seq 10
do
#判断数字是否为10
if [ ${name} -lt 10 ]
then
name="0"$name
fi
#生成用户
useradd oldboy$name
PASSWORD=$(getpassword 10)
echo $PASSWORD|passwd --stdin oldboy$name &2>/dev/null
echo -e "用户名:" oldboy$name "密码:" $PASSWORD " \n " >> name_password.txt
done
cat name_password.txt\

原文地址:https://blog.51cto.com/11381919/2096923