#!/bin/bash
read -p "请输入你想创建用户的前缀:" prefix
[ -z $prefix ] && echo "必须输入前缀" && exit # 控制前缀不能为空
[[ ! $prefix =~ ^[a-Z]+$ ]] && echo "请输入正确的前缀" && exit # 控制前缀为字母
read -p "请输入你想创建用户的个数:" num
expr 10 + $num &> /dev/null
[ $? -ne 0 ] && echo "请输入整数" && exit
for i in `seq $num`
do
echo $prefix$i
done
read -p "删除或者创建以上用户[y创建/d删除]:" re
if [ $re = "y" ];then
for i in `seq $num`
do
user=$prefix$i
# 判断用户是否存在
id $user &> /dev/null
if [ $? -eq 0 ];then
echo "该用户已经存在了,无法创建"
else
useradd $user
[ $? -eq 0 ] && echo "$user 创建成功"
fi
done
elif [ $re = d ];then
for i in `seq $num`
do
user=$prefix$i
id $user &> /dev/null
# 判断用户是否存在
if [ $? -eq 0 ];then
userdel -r $user
[ $? -eq 0 ] && echo "$user 删除成功"
else
echo "$user不存在,不需要进行删除"
fi
done
fi
标签:批量,删除,创建,echo,prefix,num,user,&&
From: https://www.cnblogs.com/9Dusk/p/18368274