shell脚本批量创建用户
#!/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\tPaassword" >> $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
其中用到的知识点:
echo $RANDOM 随机生成一串数字
md5sum将某串数字或字符转成长串字符
cut -c 1-8 取1到8位字符