我正在编写一个bash脚本来添加编辑和删除用户。我无法让它发挥作用。任何人都可以看到我错在哪里?我已经通过创建使用.sh保存的空文档将.sh文件添加到指定的文件夹中。当我输入选项1或2时,它只是要求我再次输入选项,而选项3读取ERROR。 这是我的升级代码 问题是我无法创建文件
主菜单代码
#!/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
答案 0 :(得分:0)
不得不在答案中写这个位,因为它需要一个更详细的例子,并且是调用其他可执行文件的一般设计考虑因素。
在子菜单中,询问您是否要返回主菜单。您不应该回拨Menu.sh
,因为您要添加其他图层或子流程。相反,你应该允许,例如CreateMenu.sh
只需正常退出,然后返回Menu.sh
,此时您break
循环select
,然后使用{{1}返回顶部}循环再次显示菜单。这是代码:
Menu.sh
while
CreateUser.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