英文:
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 "Enter the full path of the user file:"
read user_file
# Check if the file exists
if [ -f "$user_file" ]; then
while IFS=, read -r username full_name password main_group work_dir; do
# Create the user
sudo useradd -m -p "$password" -g "$main_group" -d "$work_dir" "$username"
# Create the user's personal directories
sudo mkdir "$work_dir/documents"
sudo mkdir "$work_dir/images"
sudo mkdir "$work_dir/videos"
sudo mkdir "$work_dir/music"
# Set appropriate permissions for the personal directories
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 "Users have been created successfully."
else
echo "The user file does not exist."
fi
# Function to delete users
delete_users() {
echo "Enter the full path of the user file:"
read user_file
# Check if the file exists
if [ -f "$user_file" ]; then
while IFS=, read -r username _ _ _ _; do
# Delete the user
sudo userdel -r "$username"
done < "$user_file"
echo "Users have been deleted successfully."
else
echo "The user file does not exist."
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 "----- MENU -----"
echo "1. Create Users"
echo "2. Delete Users"
echo "0. Exit"
echo "----------------"
echo "Enter an option:"
read option
case $option in
1)
create_users
;;
2)
delete_users
;;
0)
echo "Goodbye!"
exit 0
;;
*)
echo "Invalid option. Please try again."
;;
esac
done
}
Call the function to show the menu
show_menu
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论