linux Centos 批量创建用户测试脚本
By:Roy.LiuLast updated:2020-06-17
linux Centos 批量创建用户测试脚本, 一次行初始化多个脚本,测试直接用的循环。其实业可以从文件中读取创建。
#!/bin/bash DATE=$(date +%F_%T) USER_FILE=user.txt echo_color(){ if [ $1 == "green" ]; then echo -e "\033[32;40m$2\033[0m" elif [ $1 == "red" ]; then echo -e "\033[31;40m$2\033[0m" fi } # 如果用户文件存在并且大小大于 0就备份 if [ -s $USER_FILE ]; then mv $USER_FILE ${USER_FILE}-${DATE}.bak echo_color green "$USER_FILE exist, rename ${USER_FILE}-${DATE}.bak" fi echo -e "User\tPassword" >> $USER_FILE echo "----------------" >> $USER_FILE for USER in user{1..10}; do if ! id $USER &>/dev/null; then PASS=$(echo $RANDOM |md5sum |cut -c 1-8) useradd $USER echo $PASS |passwd --stdin $USER &>/dev/null echo -e "$USER\t$PASS" >> $USER_FILE echo "$USER User create successful." else echo_color red "$USER User already exists!" fi done
结果如下:
[root@bigdata120 shell]# ./create_user.sh ./create_user.sh: line 7: user.txt exist, rename user.txt-2020-06-17_02:27:09.bak command not found user1 User create successful. user2 User create successful. user3 User create successful. user4 User create successful. user5 User create successful. user6 User create successful. user7 User create successful. user8 User create successful. user9 User create successful. user10 User create successful.
From:一号门
Previous:Linux Centos Iptables 的一些常用方法
COMMENTS