gpt4 book ai didi

linux - Bash 脚本添加、删除用户?

转载 作者:太空宇宙 更新时间:2023-11-04 04:16:10 25 4
gpt4 key购买 nike

我目前正在尝试创建一个脚本,在运行这个特定的 bash 脚本时向任何特定的人显示一个菜单。在菜单上,我允许使用三个选项(1.添加用户,2.删除用户和3.退出)。关于添加用户选项,一旦选择,应该有 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

最佳答案

了解如何将大脚本分成小部分。接下来的代码距离好代码还很遥远,但您可以了解如何继续。

#!/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

关于linux - Bash 脚本添加、删除用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16313994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com