Bash 脚本添加、删除用户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/16313994/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash Script Add, Delete users?
提问by user2265307
i am currently trying to create a script which presents any particular person with a menu when running this particular bash script. On the menu i have allowed three options (1. Add user, 2. Delete user and 3.Quit). In relation to the add user option once selected there should be 2 options which allow the user to manually input details for a new user or add multiple users via a text file. I have been able to create the menus/options described above although i have been unable to create the script/body of the tasks for the add user manually and many users via txt file aswell as delete user.
我目前正在尝试创建一个脚本,该脚本在运行此特定 bash 脚本时向任何特定人员显示菜单。在菜单上,我允许三个选项(1. 添加用户,2. 删除用户和 3. 退出)。关于一旦选择添加用户选项,应该有 2 个选项允许用户手动输入新用户的详细信息或通过文本文件添加多个用户。我已经能够创建上述菜单/选项,尽管我无法为手动添加用户和许多用户通过 txt 文件以及删除用户创建任务的脚本/主体。
Any help with my code would be much appreciated! thanks a lot!
对我的代码的任何帮助将不胜感激!多谢!
Here is my script as of now :
这是我现在的脚本:
    #!/bin/bash
clear
echo "########## MENU ############\n"
options=("add_user" "delete_user" "exit")
select opt in "${options[@]}"
do
case $opt in
    "add_user")
        clear
    while [ 1 ]
do
  clear
  echo "1. Add user manually"
  echo "2. Add user via TXT file"
  read -p "Enter your choice" ch
  case $ch in 
      1)
        read -p "Enter user name : " useradd
       read -p "Enter user password:" passwd
    echo -e "Successfully added the user"
    ;;
    2)
   if [ $(id -u) -eq 0 ]; then
  for row in `more `
  do
  username=${row%:*}
  password=${row#*:}
 egrep "^$username" /etc/passwd >/dev/null
 if [ $? -eq 0 ]; then
    echo "$username exists!"
    exit 1
 else
    pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
    useradd -m -p $pass $username
    [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a 
user!"    
fi
done
else
    echo "Only root may add a user to the system"
    exit 2
fi    
esac  
done
    ;;
    "delete_user")
        read -p "Enter a User name to delete "UNAME
        passwd
        deluser $UNAME
        echo "User $UNAME has been deleted"         
        ;;
    "exit")
        break
        ;;
    esac
done
回答by jm666
For getting idea how to break an big script into small parts. The next is farfrom an good code, but you can get the idea, how to continue.
了解如何将大脚本分成小部分。下一个远不是一个好的代码,但你可以得到想法,如何继续。
#!/bin/bash
setps3() {
    PS3=" (press enter for choices)>"
}
setmainprompt() {
    setps3 "Main menu"
}
setaddprompt() {
    setps3 "ADD USER menu"
}
pressenter() {
    read -p "Press enter to continue:" _tmp
}
err() {
    echo 1>&2 ${CMDNAME} ERROR: $*
    return 1
}
msg() {
    echo ${CMDNAME} NOTICE: $*
    return 0
}
badchoice() {
    err "Bad choice!";
    return 1
}
do_add_one_user() {
    _name=""
    _pass=""
    echo "add code here for adding one user: ${_name} ${_pass}"
    pressenter
}
do_manual_add() {
    read -p "Username:" _username
    read -sp "Passowrd:" _password
    echo
    do_add_one_user "${_username}" "${_password}"
}
do_txt_add() {
    read -p "Filename:" _filename
    [[ -z "${_filename}" ]] && return
    echo "add code here for adding users from ${_filename}"
    echo "The code should call do_add_one_user function for every line"
    pressenter
}
do_adduser() {
    setaddprompt
    _arr_add=("Add manually" "Add via TXT" "return to main menu" "exit program")
    select add_action in "${_arr_add[@]}"
    do
        case "$REPLY" in
            1) do_manual_add ;;
            2) do_txt_add ;;
            3) return ;;
            4) exit 0 ;;
            *) badchoice ;;
        esac
        setaddprompt
    done
}
do_deleteuser() {
    echo "enter code for deleting user here"
    pressenter
}
uidcheck() {
    owner=${owner:-$(/usr/bin/id -u)}
    if [ "$owner" != "0" ]; then
        err 'Must be root'
        exit 1
    fi
}
## MAIN PROGRAM
CMDNAME=$(basename ##代码##)
#uidcheck   #uncomment when need
_arr_main=("Add user" "Delete user" "Exit program")
setmainprompt
select main_action in "${_arr_main[@]}"
do
    case "$REPLY" in
        1) do_adduser ;;
        2) do_deleteuser  ;;
        3) exit 0 ;;
        *) badchoice ;;
    esac
    setmainprompt
done

