#!/bin/bash
#建立用户生成随机密码

PASS=`openssl rand -base64 20 | cut -c 5-15`
user_name=$1
if [ "$user_name" == "" ];then
  echo "!!User name is null,please check!!"
  exit
fi
id $user_name &>/dev/null
if [ $? -eq 0 ];then
  read -p  "$user_name is exist,whether to repair $user_name password [y/n]" REPA
    if [ "$REPA" = "y" ];then
      :
    elif [ "$REPA" = "n" ];then
      echo "!!Exit!!"
      exit 10
    else
      echo "!!Error,exit!!"
      exit 10
    fi 
else
  read -p "$user_name is not exist,whether to set up [y/n]" USET
  if [ "$USET" = "y" ];then
    echo "set up user now"
    useradd $user_name &>/dev/null
  elif [ "$USET" = "n" ];then
    echo "!!Exit!!"
    exit 10
  else
    echo "!!error,exit!!"
    exit 10
  fi
fi

echo "$PASS" | passwd --stdin $user_name &>/dev/null
echo "The $user_name password is:$PASS"
