生成用户账户密码的shell

这是一个添加用户的小脚本,详情请看代码的注释

#!/bin/bash

## @author enum.Lin
## @desc   这是一个自动生成用户和密码,并可以将用户添加到sudoers 列表中的shell
#########  需要 root 的权限运行

## 带错误信息的退出函数 ##############
function exitWithErrorMsg() {
    echo -e $1
    read andKey
   exit 1
}

# 收集用户名
echo "please input your username..."
read username

# 检测用户是否存在
USER_COUNT=`cat /etc/passwd | grep "^$username:" -c`
if [ $USER_COUNT -ne 0 ]; then
    exitWithErrorMsg "The account [$username] is exist.";
fi

# 收集用户密码
echo "please first input your paassword..."
read -s firstPassword
echo "please second input your username..."
read -s secondPassword

# 检查两次密码输入是否一致
if [ $firstPassword != $secondPassword ]; then
   exitWithErrorMsg "two input password is not sample!\ninput andy key exit shell...";
fi

# 是否加入到sudoers 文件中
echo "Is add to sudoers yes/no?no"
read isSudoer

# 新增和修改用户密码
adduser $username
echo "$firstPassword" | passwd --stdin "$username"

# 添加到 sudoers 列表
if [ "$isSudoer" = "yes" ]; then
   sudo echo "$username  ALL=(ALL:ALL)    ALL" >> /etc/sudoers
   echo "add user into sudoers list"
fi

## 生成报告
echo "create user is success!"
echo -e "username:$username\npaassword:$firstPassword\nadd2SudoerFlag:$isSudoer"

echo "输入任意键,退出界面,退出前,请保存好生成的账号密码!"
read anyKey

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章