帮我用Linux shell 写一个用户管理系统
时间: 2024-01-03 22:05:32 浏览: 39
好的,以下是一个简单的用户管理系统的Linux shell脚本:
```shell
#!/bin/bash
while true
do
clear
echo "User Management System"
echo "1. Add User"
echo "2. Delete User"
echo "3. Modify User"
echo "4. View User List"
echo "5. Exit"
echo -n "Please enter your choice: "
read choice
case $choice in
1) # add user
echo -n "Please enter the username: "
read username
echo -n "Please enter the password: "
read password
sudo useradd -m -s /bin/bash $username
echo "$username:$password" | sudo chpasswd
echo "User $username has been added successfully."
read -p "Press [Enter] key to continue..."
;;
2) # delete user
echo -n "Please enter the username to delete: "
read username
sudo userdel -r $username
echo "User $username has been deleted successfully."
read -p "Press [Enter] key to continue..."
;;
3) # modify user
echo -n "Please enter the username to modify: "
read username
echo -n "Please enter the new password: "
read password
echo "$username:$password" | sudo chpasswd
echo "User $username has been modified successfully."
read -p "Press [Enter] key to continue..."
;;
4) # view user list
sudo cat /etc/passwd | awk -F: '{ print $1 }'
read -p "Press [Enter] key to continue..."
;;
5) # exit
exit 0
;;
*) # invalid option
echo "Invalid option. Please try again."
read -p "Press [Enter] key to continue..."
;;
esac
done
```
这个脚本提供了以下功能:
1. 添加用户
2. 删除用户
3. 修改用户密码
4. 查看用户列表
5. 退出程序
使用该脚本需要使用管理员权限,因此需要使用 `sudo` 命令执行操作。