linux[批量]添加,删除用户操作

添加用户

从文件中读取将要创建的用户名

account201.txt 内容:

linux[批量]添加,删除用户操作

 

 


#!/bin/bash

export PATH=/bin:/sbin:/usr/bin:/usr/sbin
# check account.txt is exits
从文件中读取将要创建的用户名
account_file="account201.txt"
# 如果文件不存在
if [ ! -f $account_file ]; then
    echo "Don‘t have accout.txt"
    exit 1
fi

usernames=$( cat $account_file )

for username in $usernames
do
  ·# 添加用户
    useradd $username
  # 设置 密码同用户名
    echo $username:$username | chpasswd
  # 设置新用户在第一次登陆时自己重置密码
    chage -d 0 $username
    echo -e "echo -e \"\033[36m made by lsx \033[0m\"" >> /home/$username/.bashrc
done
echo "Create accounts successfully!"

 

 

批量删除:

del_accounts.txt 内容

linux[批量]添加,删除用户操作

 

 

 

#!/bin/bash#houyanzhi create it to delete student‘s PUTTY accounts last year
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
# check account.txt is exits
account_file="del_account.txt"
if [ ! -f $account_file ]; then
    echo "Don‘t have accout.txt"
    exit 1
fi

usernames=$( cat $account_file )

for username in $usernames
do
  # 删除语句
userdel -r $username done echo "Delete accounts successfully!"

 

 

linux[批量]添加,删除用户操作

上一篇:linux常用代码详细作用


下一篇:关于 Shell 脚本