# This script presents a menu of options for managing users on the system. The options available include: # - Viewing the groups a user is in # - Adding a user to a group # - Changing a user's password # - Creating a new user # - Deleting an existing user # - Exit the script . colors display_menu() { options="Change my password,Change other user's password,View my group memberships,View other user's group membership,List all groups,Create new group,Delete group,Join group,Leave group,Add other user to group,Remove other user from group,Create new user,Delete user,Exit" view_my_groups="groups" view_other_groups="read -p \"Enter username: \" username; groups \$username" change_my_password="passwd" change_other_password="read -p \"Enter username: \" username; sudo passwd \$username" list_groups="cut -d: -f1 /etc/group" create_group="read -p \"Enter new group name: \" groupname; sudo groupadd \$groupname" delete_group="read -p \"Enter group to delete: \" groupname; sudo groupdel \$groupname" join_group="read -p \"Enter group name to join: \" groupname; sudo usermod -a -G \$groupname \$USER" leave_group="read -p \"Enter group name to leave: \" group; sudo gpasswd -d \$USER \$group" add_user_to_group="read -p \"Enter username: \" username; read -p \"Enter group name to add them to: \" group; sudo usermod -a -G \$group \$username" remove_user_from_group="read -p \"Enter username: \" username; read -p \"Enter group to remove them from: \" group; sudo gpasswd -d \$username \$group" create_user="read -p \"Enter new username: \" username; sudo useradd \$username; printf \"Please remember to set a password for the new user with the 'Change other user's password' option.\"" delete_user="read -p \"Enter username of user to delete: \" username; sudo userdel \$username" my_pid=$$ exit="kill -2 $my_pid" # Commands are run with 'eval' by the menu script, so we can't simply say 'exit'. The keyword $$ gets this scripts PID and the -2 code is SIGINT aka Ctrl-C commands="$change_my_password,$change_other_password,$view_my_groups,$view_other_groups,$list_groups,$create_group,$delete_group,$join_group,$leave_group,$add_user_to_group,$remove_user_from_group,$create_user,$delete_user,$exit" menu "User management options:" "$options" "$commands" } # Catch Ctrl-C and exit handle_ctrl_c() { trap 'echo exiting; exit' INT } handle_ctrl_c while true; do display_menu done