shell script 编程(2)>>批量创建账号

本文介绍了一个使用bash脚本批量创建用户账号的方法。通过简单的交互式输入,脚本能够生成一系列具有特定格式的用户名,并为每个用户分配随机密码。此外,脚本还包含了将新创建的用户添加到系统及更新密码数据库的部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习了鸟哥的私房菜,看到帐号的批处理,就试了一遍,以下是部分代码

#!/bash/bin
#建立一批账号
#2018/8/13
export PATH=/bin:/usr/bin:/sbin:/usr/sbin
accountfile="user.passwd"
#账号信息的输入
read -p "账号开头代码为 " username_start
read -p "账号年级为 " username_degree
read -p "账号数字位数为 " nu_nu

...

 -f "$accountfile" ] && mv $accountfile "$accountfile"$(date +%Y%m%d)
nu_end=$(($nustart+$nu_amount-1))
for((i=$nu_start ; i<=$nu_end ; i++))
do
        nu_len=${#i}     #获取i的位数

        if [ $nu_nu -lt $nu_len ];then
                echo "数字位数大于所规定的位数,程序将退出!"
                exit 1
        fi
        nu_diff=$(($nu_nu-$nu_len))
        if [ "$nu_diff" != "0" ];then
                nu_nn=0000000000
                nu_nn=${nu_nn:1:$nu_diff} #代表从nu_nu字符串中第1位开始取出$nu_diff个字符
        fi
        account=${username_start}${username_degree}${nu_nn}${i}
        if [ "$pwm" == "1" ];then
                passwd=$account
        else
                passwd=$(openssl rand -base64 6)
        fi
        echo "$account":"$passwd" | tee -a "$accountfile"
done
cat "$accountfile" | cut -d ":" -f 1 | xargs -n 1 useradd -m
chpasswd < "$accountfile"
pwconv
echo "OK"

转载于:https://www.cnblogs.com/fjw3214/p/10705150.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值