Bash脚本添加,删除用户?

时间:2013-05-01 06:54:18

标签: linux bash

我正在尝试创建一个脚本,在运行此特定bash脚本时向任何特定人员显示菜单。在菜单上我允许三个选项(1.添加用户,2。删除用户和3.Quit)。关于添加用户选项一旦选择,应该有2个选项允许用户手动输入新用户的详细信息或通过文本文件添加多个用户。我已经能够创建上述菜单/选项,虽然我无法手动为添加用户创建任务的脚本/正文,许多用户通过txt文件以及删除用户。

对我的代码的任何帮助将不胜感激!非常感谢!

以下是我的脚本:

    #!/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 $1`
  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

1 个答案:

答案 0 :(得分:0)

了解如何将大型剧本分解成小部分。接下来是一个好的代码,但是你可以理解,如何继续。

#!/bin/bash

setps3() {
    PS3="$1 (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="$1"
    _pass="$2"
    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 $0)
#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