shell脚本批量添加用户的脚本

好不容易放假了,重新写个批量添加用户的脚本。
思路有些混沌了,还望大家多多指教。

这个脚本还有很多地方需要改进,比如算法,比如数据的处理。
希望大家不吝提出修改意见。

  1. #!/bin/sh
  2. # Name: useraddmuti
  3. # Descripton: To add users to your system. Users can be list in a file.
  4. #             To exec this command your ID must be 0.
  5. # Author: PopZslam@Linux.net
  6. #———————————–
  7. chkUID(){
  8. getUID(){
  9. id|sed -e ‘s/(.*$//’ -e ‘s/^uid=//’
  10. }
  11. if [ “`getUID`” -ne 0 ]
  12. then
  13. echo -e ” You are not root!”
  14. exit 0
  15. fi
  16. }
  17. chkUID
  18. usagePRT(){
  19. echo ${USAGE:=’USAGE:’ `basename $0` ‘-f namelistfile’}
  20. }
  21. chkFILE(){
  22. if [ ! -z “`awk ‘NF!=2{print NF;exit;}’ $1`” ] && [ “`awk ‘NF!=2{print NF;exit;}’ $1`” -ne 2 ] ; then
  23. echo -e “The file’s format is not right!”
  24. exit 0
  25. fi
  26. }
  27. userCHK(){
  28. for USER in `awk ‘{print $1;}’ $1`
  29. do
  30. if grep -wq $USER /etc/passwd ; then
  31. echo -e “The user($USER) has been added!”
  32. exit 1
  33. fi
  34. if echo $USER|grep -wq “^[0-9].*” ; then
  35. echo -e “The user($USER)’s name is wrong format!”
  36. exit 1
  37. fi
  38. done
  39. }
  40. setOPT(){
  41. echo -e “Now Let’s set some options or you can use default settings.”
  42. setGRPNAME(){
  43. while :
  44. do
  45. echo -e “Would you like to add a new group to add these users to it?”
  46. echo -e “Enter YES to create a new group otherwise you must verify the group.”
  47. printf “Your Answer: “
  48. read grpopt
  49. case $grpopt in
  50. yes)
  51. printf “Please enter the group’s name: “
  52. read grpoptnew
  53. if cat /etc/group|sed ‘s/:.*//’|grep -wq $grpoptnew ; then
  54. echo “The group’s name($grpoptnew) exist.”
  55. exit
  56. else
  57. grpname=$grpoptnew
  58. echo -e “All these users will be added to group($grpname)…”
  59. echo -e “Adding group …”
  60. if cp /etc/group /etc/group.$$ >; /dev/null 2>;&1 ; then
  61. if groupadd $grpname ; then
  62. echo -e “The group($grpname) is added!”
  63. rm -f /etc/group.$$
  64. break 1
  65. else
  66. echo -e “There’s something wrong when adding the group($grpname).”
  67. echo -e ” *** Please recovered the group file. *** “
  68. echo -e “You can cp /etc/group.$$ to /etc/group to recover.”
  69. fi
  70. else
  71. echo “Error! Please check the program or your disk space.”
  72. exit 0
  73. fi
  74. fi
  75. ;;
  76. *) : ;;
  77. esac
  78. done
  79. }
  80. setGRPNAME
  81. }
  82. addUSER(){
  83. if cp /etc/passwd /etc/passwd.$$ && cp /etc/shadow /etc/shadow.$$ ; then
  84. for user in `sed ‘s/ .*//’ $1`
  85. do
  86. pass=`awk ‘{
  87. $1~/$name/
  88. {print $2;exit}
  89. } name=$user’ $1`
  90. if [ -z “$pass” ] ; then
  91. echo -e “The passwd is used by default sun123.”
  92. pass=sun123
  93. fi
  94. if [ ${#pass} -lt 6 ] ; then
  95. echo -e “The user($user)’s password is too short!”
  96. echo -e “Use default password: sun123.”
  97. pass=sun123
  98. fi
  99. if useradd $user ; then
  100. echo -e “The user($user) is added.”
  101. if echo $pass|passwd $user –stdin >; /dev/null 2>;&1 ; then
  102. echo -e “The user($user)’s password is setted!”
  103. else
  104. echo -e “The user($user)’s password is NOT set!”
  105. fi
  106. else
  107. echo -e “The user($user) is NOT add.”
  108. fi
  109. done
  110. rm -f /etc/passwd.$$ /etc/shadow.$$
  111. else
  112. echo -e “There something wrong when backup the passwd and shadow file.”
  113. fi
  114. }
  115. if [ $# -ne 2 ] ;  then
  116. usagePRT
  117. exit 0
  118. fi
  119. case “$1” in
  120. -f)
  121. if [ -f “$2” ] ; then
  122. echo -e “Reading usernamelist file””(“$2″)” “…”
  123. chkFILE $2
  124. userCHK $2
  125. setOPT
  126. addUSER $2
  127. else
  128. echo -e “There’s no usernamelist file!”
  129. fi
  130. ;;
  131. *) usagePRT
  132. exit 0
  133. ;;
  134. esac

转自: http://bbs.chinaunix.net/viewthread.php?tid=490674&highlight=%C5%FA%C1%BF%CC%ED%BC%D3%D3%C3%BB%A7

相关文章

发表评论

返回顶部