[root@jenkins script]# cat user_create.sh
#!/bin/bash
while : ## : 等于真 死循环
do
read -p "please enter prefix & pass & num[czq 123 5]: " prefix pass num
printf "user information:
----------------------------------
user prefix: $prefix
user password: $pass
user number: $num
----------------------------------
"
if [[ $num =~ ^[0-9]+$ ]] &>/dev/null ;then ##判断输入的num是不是数字
echo -ne "\e[1;33mAre you sure?[y/n]: \e[0m"
read action
if [ "$action" = "y" ];then ##判断用户是输入的是不是y
break
fi
else
echo "用户数量请输入数字!!"
fi
done
for i in `seq $num`
do
user=$prefix$i
id $user &>/dev/null
if [ $? -ne 0 ];then
useradd $user &>/dev/null
echo "$pass" | passwd --stdin $user &> /dev/null
echo "$user create success"
else
echo "$user exits "
fi
done
测试脚本
[root@jenkins script]# sh user_create.sh
please enter prefix & pass & num[czq 123 5]: lol redhat qqwwq
user information:
----------------------------------
user prefix: lol
user password: redhat
user number: qqwwq
----------------------------------
用户数量请输入数字!! ##数量不输入数字会提示一下
please enter prefix & pass & num[czq 123 5]: yxj cloud 3
user information:
----------------------------------
user prefix: yxj
user password: cloud
user number: 3
----------------------------------
Are you sure?[y/n]: y
yxj1 create success
yxj2 create success
yxj3 create success
[root@jenkins script]# sh user_create.sh
please enter prefix & pass & num[czq 123 5]: lol redhat 5 ##用户名lol 密码redhat 数量5个
user information:
----------------------------------
user prefix: lol
user password: redhat
user number: 5
----------------------------------
Are you sure?[y/n]: y
lol1 create success
lol2 create success
lol3 create success
lol4 create success
lol5 create success
[root@jenkins script]# id lol1 ##用户都创建成功
uid=1089(lol1) gid=1089(lol1) 组=1089(lol1)
[root@jenkins script]# id lol2
uid=1090(lol2) gid=1090(lol2) 组=1090(lol2)
[root@jenkins script]# id lol3
uid=1091(lol3) gid=1091(lol3) 组=1091(lol3)
[root@jenkins script]# id lol4
uid=1092(lol4) gid=1092(lol4) 组=1092(lol4)
[root@jenkins script]# id lol5
uid=1093(lol5) gid=1093(lol5) 组=1093(lol5)
[root@jenkins script]# useradd qew4 ##提前创建一个用户
[root@jenkins script]# sh user_create.sh
please enter prefix & pass & num[czq 123 5]: qew 123456 10
user information:
----------------------------------
user prefix: qew
user password: 123456
user number: 10
----------------------------------
Are you sure?[y/n]: y
qew1 create success
qew2 create success
qew3 create success
qew4 exits ##已经创建过的用户会提示已存在
qew5 create success
qew6 create success
qew7 create success
qew8 create success
qew9 create success
qew10 create success
网友评论