在Linux脚本中创建选择菜单。

huangapple go评论54阅读模式
英文:

Make a selection menu in a linux script

问题

以下是代码部分的翻译:

#!/bin/bash

# 创建用户的函数
create_users() {
    echo "输入用户文件的完整路径:"
    read user_file

    # 检查文件是否存在
    if [ -f "$user_file" ]; then
        while IFS=, read -r username full_name password main_group work_dir; do
            # 创建用户
            sudo useradd -m -p "$password" -g "$main_group" -d "$work_dir" "$username"

            # 创建用户的个人目录
            sudo mkdir "$work_dir/documents"
            sudo mkdir "$work_dir/images"
            sudo mkdir "$work_dir/videos"
            sudo mkdir "$work_dir/music"

            # 为个人目录设置适当的权限
            sudo chown "$username:$main_group" "$work_dir/documents"
            sudo chown "$username:$main_group" "$work_dir/images"
            sudo chown "$username:$main_group" "$work_dir/videos"
            sudo chown "$username:$main_group" "$work_dir/music"
            sudo chmod 700 "$work_dir/documents"
            sudo chmod 700 "$work_dir/images"
            sudo chmod 700 "$work_dir/videos"
            sudo chmod 700 "$work_dir/music"
        done < "$user_file"
        echo "用户已成功创建。"
    else
        echo "用户文件不存在。"
    fi
}

# 删除用户的函数
delete_users() {
    echo "输入用户文件的完整路径:"
    read user_file

    # 检查文件是否存在
    if [ -f "$user_file" ]; then
        while IFS=, read -r username _ _ _ _; do
            # 删除用户
            sudo userdel -r "$username"
        done < "$user_file"
        echo "用户已成功删除。"
    else
        echo "用户文件不存在。"
    fi
}

如何为此创建一个选择/管理菜单。

英文:
#!/bin/bash

# Function to create users
create_users() {
    echo &quot;Enter the full path of the user file:&quot;
    read user_file

    # Check if the file exists
    if [ -f &quot;$user_file&quot; ]; then
        while IFS=, read -r username full_name password main_group work_dir; do
            # Create the user
            sudo useradd -m -p &quot;$password&quot; -g &quot;$main_group&quot; -d &quot;$work_dir&quot; &quot;$username&quot;

            # Create the user&#39;s personal directories
            sudo mkdir &quot;$work_dir/documents&quot;
            sudo mkdir &quot;$work_dir/images&quot;
            sudo mkdir &quot;$work_dir/videos&quot;
            sudo mkdir &quot;$work_dir/music&quot;

            # Set appropriate permissions for the personal directories
            sudo chown &quot;$username:$main_group&quot; &quot;$work_dir/documents&quot;
            sudo chown &quot;$username:$main_group&quot; &quot;$work_dir/images&quot;
            sudo chown &quot;$username:$main_group&quot; &quot;$work_dir/videos&quot;
            sudo chown &quot;$username:$main_group&quot; &quot;$work_dir/music&quot;
            sudo chmod 700 &quot;$work_dir/documents&quot;
            sudo chmod 700 &quot;$work_dir/images&quot;
            sudo chmod 700 &quot;$work_dir/videos&quot;
            sudo chmod 700 &quot;$work_dir/music&quot;
        done &lt; &quot;$user_file&quot;
        echo &quot;Users have been created successfully.&quot;
    else
        echo &quot;The user file does not exist.&quot;
    fi


# Function to delete users
delete_users() {
    echo &quot;Enter the full path of the user file:&quot;
    read user_file

    # Check if the file exists
    if [ -f &quot;$user_file&quot; ]; then
        while IFS=, read -r username _ _ _ _; do
            # Delete the user
            sudo userdel -r &quot;$username&quot;
        done &lt; &quot;$user_file&quot;
        echo &quot;Users have been deleted successfully.&quot;
    else
        echo &quot;The user file does not exist.&quot;
    fi
}

How to make a selection/management menu for this.

答案1

得分: 1

以下是要翻译的内容:

Function to show the menu and manage options

show_menu() {
    while true; do
        echo "----- 菜单 -----"
        echo "1. 创建用户"
        echo "2. 删除用户"
        echo "0. 退出"
        echo "----------------"
        echo "输入选项:"
        read option

        case $option in
            1)
                create_users
                ;;
            2)
                delete_users
                ;;
            0)
                echo "再见!"
                exit 0
                ;;
            *)
                echo "无效选项。请重试。"
                ;;
        esac
    done
}

调用函数显示菜单

show_menu
英文:

The next code should do, put it below the already written code

Function to show the menu and manage options

show_menu() {
    while true; do
        echo &quot;----- MENU -----&quot;
        echo &quot;1. Create Users&quot;
        echo &quot;2. Delete Users&quot;
        echo &quot;0. Exit&quot;
        echo &quot;----------------&quot;
        echo &quot;Enter an option:&quot;
        read option

        case $option in
            1)
                create_users
                ;;
            2)
                delete_users
                ;;
            0)
                echo &quot;Goodbye!&quot;
                exit 0
                ;;
            *)
                echo &quot;Invalid option. Please try again.&quot;
                ;;
        esac
    done
}

Call the function to show the menu

show_menu

huangapple
  • 本文由 发表于 2023年6月9日 06:41:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定