gpt4 book ai didi

bash - shell脚本用户管理程序

转载 作者:行者123 更新时间:2023-11-29 09:32:31 25 4
gpt4 key购买 nike

我正在编写一个 bash 脚本来添加编辑和删除用户。我无法让它工作。谁能看到我哪里出错了?我通过创建用 .sh 保存的空文档将 .sh 文件添加到指定的文件夹中。当我输入选项 1 或 2 时,它只是要求我再次输入选项,而选项 3 显示错误。这是我的升级代码问题是我仍然无法创建文件

主菜单代码

#!/bin/bash
trap ''2
title="////////////////////
10101010101010101010
///main user menu///
10101010101010101010
////////////////////
"
clear
echo "$title"

PS3='Select option: '
choice=("Create User" "Edit User" "Remove User" "Exit")
select cho in "${choice[@]}"
do
case $cho in
"Create User")
/home/hiphappy1/Documents/Courseworkfiles/Users.sh
;;
"Edit User")
/home/hiphappy1/Documents/Courseworkfiles/EditUser.sh
;;
"Remove User")
/home/hiphappy1/Documents/Courseworkfiles/RemoveUser.sh
;;
"Exit")
exit 0
;;
*) echo "ERROR try again"
;;
esac
done

添加用户代码

#!/bin/bash
title="////////////////////////
101010101010101010101010
//Create Multiple Users//
101010101010101010101010
////////////////////////
"
clear
echo "$title"
password="ArsenalRule"
for USERNAME in $(cat /home/hiphappy1/Documents/Courseworkfiles/Userlist.txt)
do
useradd $USERNAME -m –s /bin/bash
echo -e "${password}\n${password}"! | passwd $USERNAME
done
read -r -p "Return to menu? [Y/N] " response
case $response in
[yY])
/home/hiphappy1/Documents/Courseworkfiles/Menulist.sh
;;
[nN])
exit 0
;;
*)
echo "ERROR try again"
;;
esac
exit

编辑用户代码

#!/bin/bash
title2="//////////////////
10101010101010101010
/////Edit User//////
10101010101010101010
////////////////////
"
clear
echo "$title2"
echo "Enter username:"
read username
echo""

id $username &> /dev/null
if [ $? -eq 0 ]; then
echo "$username exists... changing Password."
else
echo "$username does not exist! Password was not changed for $username"
exit 0
fi

passwd $username
read -r -p "Return to menu? [Y/N] " response
case $response in
[yY])
/home/hiphappy1/Documents/Courseworkfiles/Menulist.sh
;;
[nN])
exit 0
;;
*)
echo "ERROR try again"
;;
esac
exit 0

删除用户代码

#!/bin/bash
title="///////////////////
10101010101010101010
////Remove User/////
10101010101010101010
////////////////////
"
clear
echo "$title"
echo -e "Users: "
cat /etc/passwd | grep “[1][0-9][0-9][0-9]” | cut -d “:” -f1
echo ""
echo -e "Select user to remove: "
read username
sudo deluser --remove-home $username
echo ""
echo " Removing"
sleep 2
echo ""
echo " $username REMOVED"
read -r -p "Return to menu? [Y/N] " response
case $response in
[yY])
/home/hiphappy1/Documents/CourseworkFiles/Menulist.sh
;;
[nN])
exit 0
;;
*)
echo "ERROR try again"
;;
esac

最佳答案

必须在答案中写下这一点,因为它需要更详细的示例,并且是调用其他可执行文件的一般设计考虑因素。

在您的子菜单中,您询问是否要返回主菜单。你不应该回拨给 Menu.sh因为您要添加另一层或子流程。相反,您应该允许例如CreateMenu.sh正常退出,这将流回 Menu.sh ,此时你break来自 select循环并使用 while 回到顶部循环再次显示菜单。这是代码:

菜单.sh

#!/bin/bash
trap ''2
title="////////////////////
10101010101010101010
///main user menu///
10101010101010101010
////////////////////
"
clear
echo "$title"
courseworkfiles="/home/hiphappy1/Documents/CourseworkFiles"
PS3='Select option: '
choice=("Create User" "Edit User" "Remove User" "Exit")
finished=0
while (( !finished )); do
select cho in "${choice[@]}"
do
case $cho in
"Create User")
"$courseworkfiles/CreateUser.sh"
break
;;
"Edit User")
"$courseworkfiles/EditUser.sh"
break
;;
"Delete User")
"$courseworkfiles/RemoveUser.sh"
break
;;
"Exit")
finished=1
break
;;
*)
echo "ERROR try again"
;;
esac
done
done

创建用户.sh

#!/bin/bash
# ...code to create user...
#
# ..echo some sort of confirmation message
#
# then do nothing, no read, no case, just let if flow back to Menu.sh

关于bash - shell脚本用户管理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47856483/

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