LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 4317|回复: 9

slackware添加用户adduser脚本欣赏

[复制链接]
发表于 2003-12-25 13:19:28 | 显示全部楼层 |阅读模式

这是slackware中的添加用户的一个脚本,感觉写的很好,就帖了上来。想和弟兄们一起学习SHELL。




  1. #!/bin/sh

  2. ##########################################################################
  3. # Program: /usr/sbin/adduser
  4. # Purpose: Interactive front end to /usr/sbin/useradd for Slackware Linux
  5. # Author:  Stuart Winter <stuart@polplex.co.uk>
  6. #          based on the original Slackware adduser by Hrvoje Dogan
  7. #          with modifications by Patrick Volkerding
  8. # Version: 1.05
  9. ##########################################################################
  10. # History #
  11. ###########
  12. # v1.05 - 04/01/03
  13. #       * Advise & prevent users from creating logins with '.' characters
  14. #         in the user name.
  15. #       * Made pending account creation info look neater
  16. # v1.04 - 09/06/02
  17. #       * Catered for shadow-4.0.3's 'useradd' binary that no longer
  18. #         will let you create a user that has any uppercase chars in it
  19. #         This was reported on the userlocal.org forums
  20. #         by 'xcp' - thanks. <sw,pjv>
  21. # v1.03 - 20/05/02
  22. #       * Support 'broken' (null lines in) /etc/passwd and
  23. #         /etc/group files <sw>
  24. #       * For recycling UIDs (default still 'off'), we now look in
  25. #         /etc/login.defs for the UID_MIN value and use it
  26. #         If not found then default to 1000 <sw>
  27. # v1.02 - 10/04/02
  28. #       * Fix user-specified UID bug. <pjv>
  29. # v1.01 - 23/03/02
  30. #       * Match Slackware indenting style, simplify. <pjv>
  31. # v1.00 - 22/03/02
  32. #       * Created
  33. #######################################################################
  34. # Syntax: adduser [<new_user_name>]
  35. #######################################################################
  36.                                                                                 
  37. # Path to files
  38. pfile=/etc/passwd
  39. gfile=/etc/group
  40. sfile=/etc/shells
  41.                                                                                 
  42. # Paths to binaries
  43. useradd=/usr/sbin/useradd
  44. chfn=/usr/bin/chfn
  45. passwd=/usr/bin/passwd
  46. chmod=/bin/chmod
  47.                                                                                 
  48. # Defaults
  49. defhome=/home
  50. defshell=/bin/bash
  51. defchmod=711 # home dir permissions - may be preferable to use 701, however.
  52.                                                                                 
  53. # Determine what the minimum UID is (for UID recycling)
  54. # (we ignore it if it's not at the beginning of the line (i.e. commented out wit
  55. h #))
  56. export recycleUIDMIN="$(grep ^UID_MIN /etc/login.defs | awk {'print $2'} 2>/dev/
  57. null)"
  58. # If we couldn't find it, set it to the default of 1000
  59. if [ -z "$recycleUIDMIN" ]; then
  60.    export recycleUIDMIN=1000  # this is the default from /etc/login.defs
  61. fi
  62.                                                                                 
  63.                                                                                 
  64. # This setting enables the 'recycling' of older unused UIDs.
  65. # When you userdel a user, it removes it from passwd and shadow but it will
  66. # never get used again unless you specify it expliticly -- useradd just
  67. # (appears to) looks at the last line in passwd and increments the uid
  68. # I like the idea of recycling uids but you may have very good reasons not to
  69. # (old forgotten confidential files still on the system could then be owned by
  70. # this new user).  We'll set this to no because this is what the original
  71. # adduser shell script did and it's what users expect.
  72. recycleuids=no
  73.                                                                                 
  74. # Function to read keyboard input.
  75. # bash1 is broken (even ash will take read -ep!), so we work around
  76. # it (even though bash1 is no longer supported on Slackware).
  77. function get_input() {
  78.   local output
  79.   if [ "`echo $BASH_VERSION | cut -b1`" = "1" ]; then
  80.     echo -n "${1} " >&2 ; # fudge for use with bash v1
  81.     read output
  82.   else # this should work with any other /bin/sh
  83.     read -ep "${1} " output
  84.   fi
  85.   echo $output
  86. }
  87.                                                                                 
  88. # Function to display the account info
  89. function display () {
  90.   local goose
  91.   goose="$(echo $2 | cut -d ' ' -f 2-)"  # lop off the prefixed argument useradd
  92. needs
  93.   echo -n "$1 "
  94.   # If it's null then display the 'other' information
  95.   if [ -z "$goose" -a ! -z "$3" ]; then
  96.     echo "$3"
  97.   else
  98.     echo "$goose"
  99.   fi
  100. }
  101.                                                                                 
  102. # Function to check whether groups exist in the /etc/group file
  103. function check_group () {
  104.   local got_error group
  105.   if [ ! -z "$@" ]; then
  106.   for group in $@ ; do
  107.     local uid_not_named="" uid_not_num=""
  108.     grep -v "$^" $gfile | awk -F: '{print $1}' | grep "^${group}$" >/dev/null 2>
  109. &1 || uid_not_named=yes
  110.     grep -v "$^" $gfile | awk -F: '{print $3}' | grep "^${group}$" >/dev/null 2>
  111. &1 || uid_not_num=yes
  112.     if [ ! -z "$uid_not_named" -a ! -z "$uid_not_num" ]; then
  113.       echo "- Group '$group' does not exist"
  114.       got_error=yes
  115.     fi
  116.   done
  117.   fi
  118.   # Return exit code of 1 if at least one of the groups didn't exist
  119.   if [ ! -z "$got_error" ]; then
  120.     return 1
  121.   fi
  122. }
  123.                                                                                 
  124. #: Read the login name for the new user :#
  125. #
  126. # Remember that most Mail Transfer Agents are case independant, so having
  127. # 'uSer' and 'user' may cause confusion/things to break.  Because of this,
  128. # useradd from shadow-4.0.3 no longer accepts usernames containing uppercase,
  129. # and we must reject them, too.
  130.                                                                                 
  131. # Set the login variable to the command line param
  132. echo
  133. LOGIN="$1"
  134. needinput=yes
  135. while [ ! -z $needinput ]; do
  136.   if [ -z "$LOGIN" ]; then
  137.     while [ -z "$LOGIN" ]; do LOGIN="$(get_input "Login name for new user []:")"
  138. ; done
  139.   fi
  140.   grep "^${LOGIN}:" $pfile >/dev/null 2>&1  # ensure it's not already used
  141.   if [ $? -eq 0 ]; then
  142.     echo "- User '$LOGIN' already exists; please choose another"
  143.     unset LOGIN
  144.   elif [ ! "$LOGIN" = "`echo $LOGIN | tr A-Z a-z`" ]; then # useradd does not al
  145. low uppercase
  146.     echo "- User '$LOGIN' contains illegal characters (uppercase); please choose
  147. another"
  148.     unset LOGIN
  149.   elif [ ! -z "$( echo $LOGIN | grep '\.' )" ]; then
  150.     echo "- User '$LOGIN' contains illegal characters (period/dot); please choos
  151. e another"
  152.     unset LOGIN
  153.   else
  154.     unset needinput
  155.   fi
  156. done
  157.                                                                                 
  158. # Display the user name passed from the shell if it hasn't changed
  159. if [ "$1" = "$LOGIN" ]; then
  160.   echo "Login name for new user: $LOGIN"
  161. fi
  162.                                                                                 
  163. #: Get the UID for the user & ensure it's not already in use :#
  164. #
  165. # Whilst we _can_ allow users with identical UIDs, it's not a 'good thing' becau
  166. se
  167. # when you change password for the uid, it finds the first match in /etc/passwd
  168. # which isn't necessarily the correct user
  169. #
  170. echo
  171. needinput=yes
  172. while [ ! -z "$needinput" ]; do
  173.   _UID="$(get_input "User ID ('UID') [ defaults to next available ]:")"
  174.   grep -v "^$" $pfile | awk -F: '{print $3}' | grep "^${_UID}$" >/dev/null 2>&1
  175.   if [ $? -eq 0 ]; then
  176.     echo "- That UID is already in use; please choose another"
  177.   elif [ ! -z "$(echo $_UID | egrep [A-Za-z])" ]; then
  178.     echo "- UIDs are numerics only"
  179.   else
  180.     unset needinput
  181.   fi
  182. done
  183. # If we were given a UID, then syntax up the variable to pass to useradd
  184. if [ ! -z "$_UID" ]; then
  185.   U_ID="-u ${_UID}"
  186. else
  187.   # Will we be recycling UIDs?
  188.   if [ "$recycleuids" = "yes" ]; then
  189.     U_ID="-u $(awk -F: '{uid[$3]=1} END { for (i=ENVIRON["recycleUIDMIN"];i in u
  190. id;i++);print i}' $pfile)"
  191.   fi
  192. fi
  193.                                                                                 
  194. #: Get the initial group for the user & ensure it exists :#
  195. #
  196. # We check /etc/group for both the text version and the group ID number
  197. echo
  198. needinput=yes
  199. while [ ! -z "$needinput" ]; do
  200.   GID="$(get_input "Initial group [ users ]:")"
  201.   check_group "$GID"
  202.   if [ $? -gt 0 ]; then
  203.     echo "- Please choose another"
  204.   else
  205.     unset needinput
  206.   fi
  207. done
  208. # Syntax the variable ready for useradd
  209. if [ -z "$GID" ]; then
  210.   GID="-g users"
  211. else
  212.   GID="-g ${GID}"
  213. fi
  214.                                                                                 
  215. #: Get additional groups for the user :#
  216. #
  217. echo
  218. needinput=yes
  219. while [ ! -z "$needinput" ]; do
  220.   AGID="$(get_input "Additional groups (comma separated) []:")"
  221.   AGID="$(echo "$AGID" | tr -d ' ' | tr , ' ')" ; # fix up for parsing
  222.   if [ ! -z "$AGID" ]; then
  223.     check_group "$AGID"  # check all groups at once (treated as N # of params)
  224.     if [ $? -gt 0 ]; then
  225.       echo "- Please re-enter the group(s)"
  226.     else
  227.       unset needinput # we found all groups specified
  228.       AGID="-G $(echo "$AGID" | tr ' ' ,)"
  229.     fi
  230.   else
  231.     unset needinput   # we don't *have* to have additional groups
  232.   fi
  233. done
  234.                                                                                 
  235. #: Get the new user's home dir :#
  236. #
  237. echo
  238. needinput=yes
  239. while [ ! -z "$needinput" ]; do
  240.   HME="$(get_input "Home directory [ ${defhome}/${LOGIN} ]")"
  241.   if [ -z "$HME" ]; then
  242.     HME="${defhome}/${LOGIN}"
  243.   fi
  244.   # Warn the user if the home dir already exists
  245.   if [ -d "$HME" ]; then
  246.     echo "- Warning: '$HME' already exists !"
  247.     getyn="$(get_input "  Do you wish to change the home directory path? (Y/n) "
  248. )"
  249.     if [ "$(echo $getyn | grep -i "n")" ]; then
  250.       unset needinput
  251.     fi
  252.   else
  253.     unset needinput
  254.   fi
  255. done
  256. HME="-d ${HME}"
  257.                                                                                 
  258. #: Get the new user's shell :#
  259. echo
  260. needinput=yes
  261. while [ ! -z "$needinput" ]; do
  262.   unset got_error
  263.   SHL="$(get_input "Shell [ ${defshell} ]")"
  264.   if [ -z "$SHL" ]; then
  265.     SHL="${defshell}"
  266.   fi
  267.   # Warn the user if the shell doesn't exist in /etc/shells or as a file
  268.   if [ -z "$(grep "^${SHL}$" $sfile)" ]; then
  269.     echo "- Warning: ${SHL} is not in ${sfile} (potential problem using FTP)"
  270.     got_error=yes
  271.   fi
  272.   if [ ! -f "$SHL" ]; then
  273.     echo "- Warning: ${SHL} does not exist as a file"
  274.     got_error=yes
  275.   fi
  276.   if [ ! -z "$got_error" ]; then
  277.     getyn="$(get_input "  Do you wish to change the shell? (Y/n) ")"
  278.     if [ "$(echo $getyn | grep -i "n")" ]; then
  279.       unset needinput
  280.     fi
  281.   else
  282.     unset needinput
  283.   fi
  284. done
  285. SHL="-s ${SHL}"
  286.                                                                                 
  287. #: Get the expiry date :#
  288. echo
  289. needinput=yes
  290. while [ ! -z "$needinput" ]; do
  291.   EXP="$(get_input "Expiry date (YYYY-MM-DD) []:")"
  292.   if [ ! -z "$EXP" ]; then
  293.     # Check to see whether the expiry date is in the valid format
  294.     if [ -z "$(echo "$EXP" | grep "^[[:digit:]]\{4\}[-]\?[[:digit:]]\{2\}[-]\?[[
  295. :digit:]]\{2\}$")" ]; then
  296.       echo "- That is not a valid expiration date"
  297.     else
  298.       unset needinput
  299.       EXP="-e ${EXP}"
  300.     fi
  301.   else
  302.     unset needinput
  303.   fi
  304. done
  305.                                                                                 
  306. # Display the info about the new impending account
  307. echo
  308. echo "New account will be created as follows:"
  309. echo
  310. echo "---------------------------------------"
  311. display "Login name.......: " "$LOGIN"
  312. display "UID..............: " "$_UID" "[ Next available ]"
  313. display "Initial group....: " "$GID"
  314. display "Additional groups: " "$AGID" "[ None ]"
  315. display "Home directory...: " "$HME"
  316. display "Shell............: " "$SHL"
  317. display "Expiry date......: " "$EXP" "[ Never ]"
  318. echo
  319.                                                                                 
  320. echo "This is it... if you want to bail out, hit Control-C.  Otherwise, press"
  321. echo "ENTER to go ahead and make the account."
  322. read junk
  323.                                                                                 
  324. echo
  325. echo "Creating new account..."
  326. echo
  327. echo
  328.                                                                                 
  329. # Add the account to the system
  330. CMD="$useradd "$HME" -m "$EXP" "$U_ID" "$GID" "$AGID" "$SHL" "$LOGIN""
  331. $CMD
  332.                                                                                 
  333. if [ $? -gt 0 ]; then
  334.   echo "- Error running useradd command -- account not created!"
  335.   echo "(cmd: $CMD)"
  336.   exit 1
  337. fi
  338.                                                                                 
  339. # Set the finger information
  340. $chfn "$LOGIN"
  341. if [ $? -gt 0 ]; then
  342.   echo "- Warning: an error occurred while setting finger information"
  343. fi
  344.                                                                                 
  345. # Set a password
  346. $passwd "$LOGIN"
  347. if [ $? -gt 0 ]; then
  348.   echo "* WARNING: An error occured while setting the password for"
  349.   echo "           this account.  Please manually investigate this *"
  350.   exit 1
  351. fi
  352.                                                                                 
  353. # If it was created (it should have been!), set the permissions for that user's
  354. dir
  355. HME="$(echo "$HME" | awk '{print $2}')"  # We have to remove the -g prefix
  356. if [ -d "$HME" ]; then
  357.   $chmod $defchmod "$HME"
  358. fi
  359.                                                                                 
  360. echo
  361. echo
  362. echo "Account setup complete."
  363. exit 0

复制代码
发表于 2003-12-25 21:40:18 | 显示全部楼层

这个东西在哪里用到?

我想用在我的LFS系统中,请问怎么弄?
 楼主| 发表于 2003-12-26 11:52:06 | 显示全部楼层
这个可以改一下吧
就是adduser,在/usr/sbin中的。

主要是看一下原来的adduser是什么样的,more /usr/sbin/adduser
然后把这些内容和原来的对照,放到原来的adduser中,不过要备份原来的文件。。。切记。。。


试试看,因为我菜的也不行
发表于 2003-12-26 12:04:48 | 显示全部楼层

回复: 这个东西在哪里用到?

最初由 bnfan 发表
我想用在我的LFS系统中,请问怎么弄?

man一下adduser,如果你有这个命令的话(我想任何一个系统都应该有这个命令吧),可以根据自己的需要按照adduser规定的格式批量添加用户!老大的脚本就是把复杂的命令行操作给简化了~~ ;)
发表于 2003-12-26 12:14:18 | 显示全部楼层
呵呵,如果不是批量操作,还是直接改/etc/passwd方便。
 楼主| 发表于 2003-12-26 12:14:23 | 显示全部楼层

回复: 回复: 这个东西在哪里用到?

最初由 javalee 发表
man一下adduser,如果你有这个命令的话(我想任何一个系统都应该有这个命令吧),可以根据自己的需要按照adduser规定的格式批量添加用户!老大的脚本就是把复杂的命令行操作给简化了~~ ;)


Lee兄&tram兄,请教一个问题,现在我也在读shell的快,进度太慢了,然后也读了不少,是把书和shell实例结合起来一起读呢,还是先读书后看例子,然后再来研究。

一边操作,一边读书???

请Lee兄&tram兄指条路吧,毕竟我的学文的,看来学的高深一点,对我可能性不太大了。感觉要认命了。  我的命怎么这么苦啊
发表于 2003-12-26 12:17:58 | 显示全部楼层
呵呵,我对shell只是一知半解,要写什么东西,都要查半天。我感觉用过的命令印象比较深
发表于 2003-12-26 12:19:09 | 显示全部楼层

回复: 回复: 回复: 这个东西在哪里用到?

最初由 北南南北 发表
Lee兄&tram兄,请教一个问题,现在我也在读shell的快,进度太慢了,然后也读了不少,是把书和shell实例结合起来一起读呢,还是先读书后看例子,然后再来研究。

一边操作,一边读书???

请Lee兄&tram兄指条路吧,毕竟我的学文的,看来学的高深一点,对我可能性不太大了。感觉要认命了。

把书上的实例跟自己的应用结合,这样理解得快些~,当然,必要的shell基础知识是最重要的,我想这个老大应该没问题!shell的主要基础在于如何灵活的操作变量!还有一些内制的shell命令,如此而已!
 楼主| 发表于 2003-12-26 12:23:49 | 显示全部楼层

回复: 回复: 回复: 回复: 这个东西在哪里用到?

最初由 javalee 发表
把书上的实例跟自己的应用结合,这样理解得快些~,当然,必要的shell基础知识是最重要的,我想这个老大应该没问题!shell的主要基础在于如何灵活的操作变量!还有一些内制的shell命令,如此而已!


多谢lee兄  tram兄指教,我会住Shell讨论区的,太多的问题要学习了,我先掀一下以往弟兄们的精华帖。以后常驻Slackware和shell了。我菜的太可以了,菜的自己都不知道是谁了。
发表于 2003-12-26 12:28:54 | 显示全部楼层

回复: 回复: 回复: 回复: 回复: 这个东西在哪里用到?

最初由 北南南北 发表
自己都不知道是谁了。

我们知道你是谁就成了~~ ;)
BTW:我终于买到了RH linux8的宝典,正翻着呢~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表