转载自:http://linux5588.blog.51cto.com/65280/750836

运行结果如下:

  1. [root@server1 shell.sh]# ./useradd.sh  

  2. Please Input 1 or 2 :  

  3. 1) Add User  

  4. 2) Del User  

  5. #? 1  

  6. Please Input your username prefix:user  

  7. Please Input your user number:10  

  8. Please Enter your user password: 123  

  9. Add user1 Sucessful!  

  10. Add user2 Sucessful!  

  11. Add user3 Sucessful!  

  12. Add user4 Sucessful!  

  13. Add user5 Sucessful!  

  14. Add user6 Sucessful!  

  15. Add user7 Sucessful!  

  16. Add user8 Sucessful!  

  17. Add user9 Sucessful!  

  18. Add user10 Sucessful!


以下是脚本内容:

  1. #!/bin/bash  

  2. #this scripts for batch add/del user  

  3. #DATE:2011/9/25 AUTHOR:LEO Email:chanyipiaomiao@163.com  

  4. #if you are not root,exit  

  5. ID=$(id -u)  

  6. if [ $ID -ne 0 ];then  

  7. echo "Your are not root,only root can Add/Del user"  

  8. exit 1  

  9. fi  

  10. #define variables  

  11. ADD="/usr/sbin/useradd"

  12. DEL="/usr/sbin/userdel"

  13. PASSWD="/usr/bin/passwd"

  14. num=1

  15. echo "Please Input 1 or 2 :"  

  16. select var in "Add User" "Del User"  

  17. do  

  18. break  

  19. done  

  20. read -p "Please Input your username prefix:" pre  

  21. read -p "Please Input your user number:" user_num  

  22. case $var in  

  23. "Add User")  

  24. read -p "Please Enter your user password: " PW  

  25. while [ $num -le $user_num ]  

  26. do  

  27.  if [ -e /home/$pre$num -o -e /var/mail/$pre$num ]  

  28.  then  

  29.       echo "$pre$num already exists!!"  

  30. num=$(expr $num + 1)  

  31.  else  

  32.   $ADD $pre$num && echo $PW | $PASSWD --stdin $pre$num &>/dev/null  

  33.   if [ $? = 0 ];then  

  34.    echo "Add $pre$num Sucessful!"  

  35.   fi  

  36. num=$(expr $num + 1)  

  37.  fi  

  38. done  

  39. ;;  

  40. "Del User")  

  41. while [ $num -le $user_num ]  

  42. do  

  43.    $DEL -r $pre$num &>/dev/null && echo "Delete $pre$num Sucessful" || echo "There is no $pre$num"  

  44. num=$(expr $num + 1)  

  45. done  

  46. ;;  

  47. *)  

  48. echo "Invaild Input"  

  49. esac