shell脚本用户管理程序

问题描述 投票:3回答:1

我正在编写一个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
bash shell shebang
1个回答
0
投票

不得不在答案中写这个位,因为它需要一个更详细的例子,并且是调用其他可执行文件的一般设计考虑因素。

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

menu.是

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

create user.是

#!/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
© www.soinside.com 2019 - 2024. All rights reserved.